johnwayne77 Posted July 20, 2007 Share Posted July 20, 2007 Hi! I'm trying to make a signup system for a project of mine. All good till now, I can register users but I want to check the input fields : USERNAME & EMAIL for duplicity in the database, so people can't register withe same username or email twice. This is my code related to this: $_utilizator = str_replace("<", "", $_POST['utilizator']); $_parola = str_replace("<", "", $_POST['parola']); $_email = str_replace("<", "", $_POST['email']); mysql_connect($host, $user_name, $password) or die('Could not connect: ' . mysql_error()); mysql_select_db($database_name) or die('Could not select database'); $result = mysql_query("SELECT * FROM users WHERE email='$_email'") or die(mysql_error()); $row = mysql_fetch_array( $result ); if(($row['email'] == '') || ($row['utilizator'] == '')) { mysql_query ("INSERT INTO users (nume, an, grupa, localitate, cnp, utilizator, parola, email, admin) VALUES ('$_nume','$_an','$_grupa','$_localitate','$_cnp','$_utilizator','$_parola', '$_email', '0')"); echo "<br><b>cont creat</b><br>"; } So, now it checks only the duplicity of email. If i change this line: $result = mysql_query("SELECT * FROM users WHERE email='$_email'") or die(mysql_error()); from email='$_email' to utilizator='$_utilizator' , it will check for the duplicity of the username but will ignore the email field. I've tried puttin in the IF statement the following: if(($row['email'] == '') || ($row['utilizator'] == '')) if($row['email'] == '' || $row['utilizator'] == '') if($row['email'] == '' && $row['utilizator'] == '') ..but with no luck. Any ideas ? Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/ Share on other sites More sharing options...
dooper3 Posted July 20, 2007 Share Posted July 20, 2007 yeah, change the query to: $result=("SELECT * FROM users WHERE email='$_email' OR utilizator='$_utilizator'") or die(mysql_error()); That's assuming the column in mysql is called "utilizator" aswell. Leave the rest as it was and it should work. It wasn't working before because you were only asking if email addresses were the same, but not usernames, and then you had your if statement querying both even though you hadn't looked in the database for both. Personally, a i think word like utilizator is really unneccessary; you're just gonna make extra problems for yourself with spelling errors leading to mysql errors etc., keep it simple! Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/#findComment-303225 Share on other sites More sharing options...
johnwayne77 Posted July 20, 2007 Author Share Posted July 20, 2007 i've changed the line with: $result = mysql_query("SELECT * FROM users WHERE email='$_email' OR utilizator='$_utilizator'") or die(mysql_error()); and the if is: if(($row['email'] == '') || ($row['utilizator'] == '')) it checks only the email duplicity... as for username, i can register 10 usernames 'michael' for example if i put utilizator='$_utilizator' before email='$_email' in the select command above, it checks only the utilizator (that is the first condition)... can't figure out what's the matter with it Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/#findComment-303233 Share on other sites More sharing options...
johnwayne77 Posted July 20, 2007 Author Share Posted July 20, 2007 and i've tried puttin AND instead of OR and the same thing Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/#findComment-303234 Share on other sites More sharing options...
DeadEvil Posted July 20, 2007 Share Posted July 20, 2007 separate checking # for duplicate name $result = mysql_query("SELECT nume FROM users WHERE nume = '$_nume' AND email='$_email'") or die(mysql_error()); $row = mysql_num_rows($result); if($row > 0): header('location: previewspage.php'); endif; # for duplicate email $result = mysql_query("SELECT email FROM users WHERE nume = '$_nume' AND email='$_email'") or die(mysql_error()); $row = mysql_num_rows($result); if($row > 0): header('location: previewspage.php'); endif; single checking # for duplicate name & email $result = mysql_query("SELECT nume, email FROM users WHERE nume = '$_nume' AND email='$_email'") or die(mysql_error()); $row = mysql_num_rows($result); if($row > 0): header('location: previewspage.php'); endif; Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/#findComment-303240 Share on other sites More sharing options...
johnwayne77 Posted July 20, 2007 Author Share Posted July 20, 2007 awsome! i've used the second option, checking both fields and it works just fine! ( mersi baros! ) Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/#findComment-303241 Share on other sites More sharing options...
dooper3 Posted July 20, 2007 Share Posted July 20, 2007 Ah! I was being stupid, that's a really bad way of doing it. As all you want to know is that there aren't any duplicates, you just need to count the number of rows with matching emails or usernames, so you should change it all to... $result=mysql_numrows(mysql_query("SELECT * FROM users WHERE email='$_email' OR utilizator='$_utilizator'")) or die(mysql_error()); if ($result > 0) { echo("Someone is already registered with that username or email address!"); } else { mysql_query ("INSERT INTO users (nume, an, grupa, localitate, cnp, utilizator, parola, email, admin) VALUES ('$_nume','$_an','$_grupa','$_localitate','$_cnp','$_utilizator','$_parola', '$_email', '0')"); echo "<br><b>cont creat</b><br>"; } That should do the trick! Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/#findComment-303243 Share on other sites More sharing options...
johnwayne77 Posted July 20, 2007 Author Share Posted July 20, 2007 heh, that works too. interesting approaches thanks Link to comment https://forums.phpfreaks.com/topic/60937-solved-signup-form-check-both-username-and-email-fields-for-duplicity-in-db/#findComment-303246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.