0
votes

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

1
Side note: Never use addslashes() to sanitize incoming data. Use mysql_real_escape_string() for the DB, and htmlentities() when outputting itPekka
addslashes is definitely wrongQuassnoi
"query was empty" usually occurs only when the query really is empty. Are you 100% sure the error message is related to the query you are showing.Pekka
Yeah, I think so. How would I know?Taimur

1 Answers

3
votes

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.