I’ve got a signup/login system on my site, and I’ve made it so that if someone tries to sign up with a username that is already in use, it posts an error underneath the form. However, instead of posting the error message that I created, it returns a MySQL error “Error: Query was empty”. Here is the code that I am trying to use to do this:
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM userpass WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
$error = "Sorry, the username ".$_POST['username']." is already in use.";
}
What am I doing wrong?
Thanks
Your query is nonsense (SELECT username … WHERE username =…)
What you must do is adding UNIQUE constraint on username and trying directly to insert the user in your database. If username does already exist the query will return an error.
and just try to insert an user with existing id.