how can check if email or username exist in mysql database while user registering stop registration process , possible suggest 2/3 new username not exist in database new user.
so tried working around , 1 check if email exist in database meanwhile need 1 check both username , email in database , print both $error separately. mean if email exist , username not exist print error email , same username.
i hope understand me.
<?php if(isset($_post['register'])) { $username = $_post['username']; $full_name = ucwords($_post['full_name']); $email = $_post['email']; $password = trim($_post['password']); $time = time(); $age = $_post['age']; $gender = $_post['gender']; // geolocation $longitude = $_session['longitude']; $latitude = $_session['latitude']; $geo_info = $geo->getinfo($latitude,$longitude); $city = $geo_info['geonames'][0]['name']; $country = $geo_info['geonames'][0]['countryname']; $check_d = $db->query("select username, email users username = '$username' or email = '$email'"); $check_d = $check_d->num_rows; if($check_d == 0) { $db->query("insert users (profile_picture,username,full_name,email,password,registered,credits,age,gender,ip,country,city,longitude,latitude) values ('default_avatar.png','$username','$full_name','$email','".$auth->hashpassword($password)."','$time','100','$age','$gender','$ip','".$country."','".$city."','".$longitude."','".$latitude."')"); setcookie('justregistered', 'true', time()+6); setcookie('mm-email',$email,time()+60*60*24*30,'/'); header('location: '.$domain.'/people'); } else { $error = 'username or password exist, try another'; } } if($auth->islogged()) { $first_name = $system->getfirstname($_session['full_name']); $logged_in_user = header('location: '.$domain.'/people'); } $users = $db->query("select * users order rand() limit 7"); ?>
you can it, checking username , email in separated queries.
in implemenation posting first check mail, check username in database. if both in db, later errormessage (for username) overwrite message email. should change that, give user more precise feedback why dataset not inserted table.
before doing of checks, initializing variable $error
null
, overwritten if 1 of constraint checks fails, can check if still null after checks decide whether insert
should executed or not.
note: should not use script, vulnerable sql injections. see thread learn should change: how can prevent sql injection in php?
$error = null; $check_mail = $db->query("select id users email='" . $email . "'"); if ($check_mail->num_rows > 0) { $error = 'email exists'; } $check_username = $db->query("select id users username='" . $username . "'"); if ($check_username->num_rows > 0) { $error = 'username exists'; } if($error === null){ $db->query("insert users (profile_picture,full_name,email,password,registered,credits,age,gender,ip,country,city,longitude,latitude) values ('default_avatar.png','$full_name','$email','" . $auth->hashpassword($password) . "','$time','100','$age','$gender','$ip','" . $country . "','" . $city . "','" . $longitude . "','" . $latitude . "')"); setcookie('justregistered', 'true', time() + 6); setcookie('mm-email', $email, time() + 60 * 60 * 24 * 30, '/'); header('location: ' . $domain . '/people'); }
Comments
Post a Comment