obiwan Posted June 11, 2008 Share Posted June 11, 2008 Hey everyone, I set up a registration page and I am trying to figure out how to alert the user that the username they have chosen is already in use I am using the end users email as the username. Please view my code below, I am new to PHP and mysql and would appreciate any help. <? // display errors ini_set('display_errors',1); error_reporting(E_ALL & ~E_NOTICE); // check passwords match before continuing if($user_password != $confirm_password){ print '<p>Passwords do not match, please correct and try again.</p>'; print '<span id="regLinks"><a href=../createAccount.htm>Click here to re-enter information</a>'; exit(); }?> <? [b]// check if username exists // line below gives me error:Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource// if(mysql_num_rows($checkUserName)>=1) { print '<p>That username already exists, please select another username.</p>'; exit(); }[/b] // if passwords do match update database and display data from db if($user_password == $confirm_password) { print '<br /><span class="title">Congratulations, you have successfully created a profile</span><br />'; print '<span class="subTitle">The following is your new log In information, you will receive a confirmation email shortly for your records.</span>'; $db_name = "auth_users"; $table_name = "site_members"; $connection = @mysql_connect("myServerInfo","myUserName","myPassWord") or die(mysql_error()); $db = @mysql_select_db($db_name,$connection) or die(mysql_error()); $checkUserName="SELECT * FROM $table_name WHERE username = '$_POST[user_email]'"; $userResult = mysql_query($checkUserName) or die(mysql_error()); $sql ="INSERT INTO $table_name(f_name,l_name,username,password,photoUrl) VALUES ('$_POST[f_name]','$_POST[l_name]','$_POST[user_email]',password('$_POST[user_password]'),'$_POST[userPhotoUrl]')"; $result = @mysql_query($sql,$connection)or die(mysql_error()); // confirmation email $to = "$_POST[user_email]"; $subject ='New Profile & Log In Information On myUrl'; // More headers $headers .= 'From: <myEmail>' . "\r\n"; $headers .= 'Cc: myEmail' . "\r\n"; // body of email // $msg = "Your Profile Log In Information on myUrl\n"; $msg .= "First Name: $_POST[f_name]\n"; $msg .= "Last Name: $_POST[l_name]\n"; $msg .= "Username: $_POST[user_email]\n"; $msg .= "Password: $_POST[user_password]\n"; mail($to,$subject,$msg,$headers); } ?> Link to comment https://forums.phpfreaks.com/topic/109692-solved-how-to-validate-username-in-mysql-database-already-exisits-using-php/ Share on other sites More sharing options...
grimmier Posted June 11, 2008 Share Posted June 11, 2008 You need to issue a query on the DB for the specific username first. $connection = @mysql_connect("myServerInfo","myUserName","myPassWord") or die(mysql_error()); $db = @mysql_select_db($db_name,$connection) or die(mysql_error()); $checkUserName="SELECT * FROM $table_name WHERE username = '$_POST[user_email]'"; $userResult = mysql_query($checkUserName) or die(mysql_error()); if (mysql_num_rows($userResult) >=1) { print '<p>That username already exists, please select another username.</p>'; exit(); } Basically you just needed to move your connection and query higher up in your code, so the query is preformed before the check. Link to comment https://forums.phpfreaks.com/topic/109692-solved-how-to-validate-username-in-mysql-database-already-exisits-using-php/#findComment-562881 Share on other sites More sharing options...
obiwan Posted June 11, 2008 Author Share Posted June 11, 2008 You need to issue a query on the DB for the specific username first. $connection = @mysql_connect("myServerInfo","myUserName","myPassWord") or die(mysql_error()); $db = @mysql_select_db($db_name,$connection) or die(mysql_error()); $checkUserName="SELECT * FROM $table_name WHERE username = '$_POST[user_email]'"; $userResult = mysql_query($checkUserName) or die(mysql_error()); if (mysql_num_rows($userResult) >=1) { print '<p>That username already exists, please select another username.</p>'; exit(); } Basically you just needed to move your connection and query higher up in your code, so the query is preformed before the check. Ok I will try this right now and see if that does the trick Link to comment https://forums.phpfreaks.com/topic/109692-solved-how-to-validate-username-in-mysql-database-already-exisits-using-php/#findComment-562883 Share on other sites More sharing options...
obiwan Posted June 11, 2008 Author Share Posted June 11, 2008 Awesome! That totally worked!! Thanks a lot I really appreciate it. Link to comment https://forums.phpfreaks.com/topic/109692-solved-how-to-validate-username-in-mysql-database-already-exisits-using-php/#findComment-562888 Share on other sites More sharing options...
grimmier Posted June 11, 2008 Share Posted June 11, 2008 make sure you check the solved button on bottom of the thread. =) so people know its fixed. Link to comment https://forums.phpfreaks.com/topic/109692-solved-how-to-validate-username-in-mysql-database-already-exisits-using-php/#findComment-562891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.