suttercain Posted August 24, 2007 Share Posted August 24, 2007 Hi guys, I have <?php $usertest = mysql_query("SELECT username FROM merchants WHERE username = '" .mysql_real_escape_string($_POST['username']). "'"); if ($usertest) { echo $_POST['username']. " is already taken. Please select a different username."; } else { //process other code } ?> What am I doing wrong. I need to check if the username is already in the database. If it is, the script stops. If not, the code process begins. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/66581-solved-basic-check-if-username-already-exists/ Share on other sites More sharing options...
AdRock Posted August 24, 2007 Share Posted August 24, 2007 what errors are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/66581-solved-basic-check-if-username-already-exists/#findComment-333508 Share on other sites More sharing options...
suttercain Posted August 24, 2007 Author Share Posted August 24, 2007 No errors, but no matter what I get that the username already exists in the database, even if it doesn't. SC Quote Link to comment https://forums.phpfreaks.com/topic/66581-solved-basic-check-if-username-already-exists/#findComment-333509 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 You need to pull the data <?php $usertest = mysql_query("SELECT username FROM merchants WHERE username = '" .mysql_real_escape_string($_POST['username']). "'"); $num_rows = mysql_num_rows($usertest); if ($num_rows >0) { echo $_POST['username']. " is already taken. Please select a different username."; } else { //process other code } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66581-solved-basic-check-if-username-already-exists/#findComment-333510 Share on other sites More sharing options...
suttercain Posted August 24, 2007 Author Share Posted August 24, 2007 Doh! Thanks madtechie! It has been a long week. Quote Link to comment https://forums.phpfreaks.com/topic/66581-solved-basic-check-if-username-already-exists/#findComment-333511 Share on other sites More sharing options...
micmania1 Posted August 24, 2007 Share Posted August 24, 2007 <?php $usertest = mysql_query("SELECT username FROM merchants WHERE username = '" .mysql_real_escape_string($_POST['username']). "'"); $user_exist= mysql_num_rows($usertest); if ($user_exist > 1) { echo $_POST['username']. " is already taken. Please select a different username."; exit(); // Exit the script } else { //process other code } ?> Got there before me lol If you want to quit the script you will need to put "exit();" on line 5 as i've done. Quote Link to comment https://forums.phpfreaks.com/topic/66581-solved-basic-check-if-username-already-exists/#findComment-333513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.