PHP - MySQL error "Error: Query was empty" when someone tries to sign up with a username that is already taken
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 me开发者_如何学Pythonssage 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.
ALTER TABLE user ADD UNIQUE (username)
and just try to insert an user with existing id.
精彩评论