hey peeps i am have a little issues my problem is that im trying to check if more than one user name exists in the database and if not do an insert statement and wen its finish echo a message stating thank u '.$user.' for registering can anyone tell me my errors $duplicate = mysql_num_rows(mysql_query("select * from user_accounts where username = '$user'")); if ($duplicate > 0){ echo "The username '.$user.' is already taken try another."; } $result= mysql_query ("INSERT INTO user_accounts (username, firstname, lastname, birthday, gender, address, password, confirm_password, email_address) VALUES ('$user', '$first', '$last', '$birth', '$sex', '$addres', '$pass', '$conpass', '$eaddress')"); echo "thank you '$user' for registering."; ?> PHP:
After you echo that the username has already been taken, it will still process the rest of your script. Either place the ELSE {} after and include the register process between { and } or place the exit function after the echo message that the username has been taken. $duplicate = mysql_num_rows(mysql_query("select * from user_accounts where username = '$user'")); if ($duplicate > 0) { echo "The username '.$user.' is already taken try another."; } ELSE { $result= mysql_query ("INSERT INTO user_accounts (username, firstname, lastname, birthday, gender, address, password, confirm_password, email_address) VALUES ('$user', '$first', '$last', '$birth', '$sex', '$addres', '$pass', '$conpass', '$eaddress')"); echo "thank you '$user' for registering.";} ?> PHP:
Yeah, but clean-up your code. And remember LIMIT 1 on your MySQL statement, since you want it to stop searching if a row is found: <? $duplicate = mysql_num_rows(mysql_query("SELECT * FROM user_accounts WHERE username = '".$user."' LIMIT 1")); if ($duplicate > 0){ echo 'The username '.$user.' is already taken try another.'; } else { $result = mysql_query ("INSERT INTO user_accounts ( username, firstname, lastname, birthday, gender, address, password, confirm_password, email_address ) VALUES ( '$user', '$first', '$last', '$birth', '$sex', '$addres', '$pass', '$conpass', '$eaddress')"); echo 'thank you '.$user.' for registering.'; } ?> PHP: You will benefit from making it right and easy to overview the first time Furthermore, remember to escape and trim the $user variable to prevent injections.