wiggly Posted April 22, 2006 Share Posted April 22, 2006 Hi i am a complete newbe to php and thus i dont know very much however i have started writing a registration/login system for my website and i can not seem to figure out how to stop duplicate usernames or email addresses i have managed to get the actualy registration part working and i just need a bit of advice on how i could stop multiple people using the same details. i did find a tutorial that i tried to use some code from on phpfreaks and i had no luck.this is the code i tried:$sql_email_check = mysql_query("SELECT email form users WHERE email_address='$email");$sql_username_check = mysql_query("SELECT username from users WHERE username='$username'");$email_check = mysql_num_rows($sql_email_check);$username_check = mysql_num_rows($sql_username_check);if(($mail_check > 0) || ($username_check > 0)){ echo "Please fix the following errors: <br />"; if($email_check > 0){ echo "<strong>Your Email Address has already been used by another member in our database please submit a diffrent Email Address!</strong> <br />"; unset($email); } if($username_check > 0){ echo "<strong>The Username you selected has alreadey been used by another member in our database. Please choose a diffrent Username</strong> <br />"; unset($username); }i would be most greatfull if someone could help me as i have been bugged for the past week with this problem, maybee i have just typed somthing wrong or missed somthing out i have no idea. Quote Link to comment https://forums.phpfreaks.com/topic/8140-duplicate-sql-entries/ Share on other sites More sharing options...
Barand Posted April 22, 2006 Share Posted April 22, 2006 You don't say what the problem is. Just posting code and saying "It doesn't work" does not give anyone much of clue about what to look for.I did notice in this line below you have used $mail_check instead of "$email_check"[code]if(($mail_check > 0) || ($username_check > 0)){[/code]Also there are a lot of old tutes out there which assume register_globals is set ON whereas with later version of php it is set OFF by default. This means you may have explicitly get the the username and email address from the $_GET or $_POST arrays at the beginning of your script[code]$username = $_POST['username'];$email = $_POST['email'];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8140-duplicate-sql-entries/#findComment-29681 Share on other sites More sharing options...
wiggly Posted April 23, 2006 Author Share Posted April 23, 2006 [code]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Server\Apache2\htdocs\login2\register.php on line 45Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Server\Apache2\htdocs\login2\register.php on line 46[/code]these are the errors i get when i use this piece of code however when i take the code out i get no errors what i want to do is add code to my register.php to stop people registering with a username and email address that is already registered.here is my code with the above problems removed maybee that will be more helpfull to you [code]<?include('include/db_connect.php');$first = $_POST['first'];$last = $_POST['last'];$email = $_POST['email'];$phone = $_POST['phone'];$user = $_POST['user'];$pass = $_POST['pass'];$info = $_POST['info'];$first = stripslashes($first);$last = stripslashes($last);$email = stripslashes($email);$phone = stripslashes($phone);$user = stripslashes($user);$pass = stripslashes($pass);$info = stripslashes($info);if((!$first) || (!$last) || (!$email) || (!$user) || (!$pass)){ echo '<strong>You must fill in all required fields!!!</strong> <br />'; if(!$first){ echo "You must fill in the First Name field! <br />"; } if(!$last){ echo "You must fill in the Last Name field! <br />"; } if(!$email){ echo "You must fill in the E-Mail field! <br />"; } if(!$user){ echo "You must fill in the Username field! <br />"; } if(!$pass){ echo "You must fill in the Password field! <br />"; } include 'register.html'; exit();}$pass=md5($pass);$sql = mysql_query("INSERT INTO users (first, last, email, phone, user, pass, info) VALUES('$first', '$last', '$email', '$phone', '$user', '$pass', '$info')") or die (mysql_error()); if(!$sql){ echo 'There has been a problem during the registration process please try again later! <br />';}else{$userid = mysql_insert_id(); echo 'Your account was created sucsesfully <br />';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8140-duplicate-sql-entries/#findComment-29842 Share on other sites More sharing options...
Barand Posted April 23, 2006 Share Posted April 23, 2006 You get those messages because of errors in the sql queries.There is certainly an error in the first, FROM is mis-spelt as FORM[code]$sql_email_check = mysql_query("SELECT email form usersWHERE email_address='$email");$sql_username_check = mysql_query("SELECT username from usersWHERE username='$username'");[/code]Use this code to get the error messages[code]$sql_email_check = mysql_query("SELECT email from usersWHERE email_address='$email") or die (mysql_error() . ' in query ' . $sql_email_check);$sql_username_check = mysql_query("SELECT username from usersWHERE username='$username'") or die (mysql_error() . ' in query ' . $sql_username_check);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8140-duplicate-sql-entries/#findComment-29870 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.