mysql - Php Ensuring a unique username -


i have register page , want ensure usernames unique. have tried solutions on here , nothing working me.

this error i'm getting:

warning:  mysqli_query() expects @ least 2 parameters, 1 given  in c:\xampp\htdocs\project\projectregistered.php on line 16 query empty 

here php:

 <?php  $con=mysqli_connect("localhost","root","","test");  // check connection  if (mysqli_connect_errno()) {      echo "failed connect mysql: " . mysqli_connect_error();  }  $username = $_post['username'];    echo $username;  $query = mysqli_query("select * projectuser username='$username'");  $result1 = mysql_query($query) or die (mysql_error().$query);     $count = mysql_num_rows($result1);  if ($count>0) { echo 'sorry! username exists!'; } else {     $surname = $_post['surname'];     $sql = "insert projectuser (firstname, surname, username, password, location)             values             ('$_post[firstname]', '".mysql_real_escape_string($surname)."', '$_post[username]', '$_post[password]', '$_post[location]')"; }  if (!mysqli_query($con,$sql)) {     die('error: ' . mysqli_error($con)); } echo "1 record added ".$sql;  mysqli_close($con);  ?> 

as you're using procedural approach, need pass connection handle first argument. should

mysqli_query($con, "select * projectuser username='$username'"); 

additionally, make username column in db table unique.

update:
realised you're using both mysql_ mysqli_ functions. better stick second one. code be:

$query = "select * projectuser username='$username'"; $result = mysqli_query($con, $query); $count = mysqli_num_rows($result); 

but mentioned above, if set username column unique won't need check @ all. use mysqli_insert_id after insert check whether record has been inserted or not. if hasn't been inserted, unique constraint had fired off. way more efficient selecting first check whether username exists.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -