Benjigga Posted August 31, 2008 Share Posted August 31, 2008 I'm making a registration page. I'm trying to get it to double check to see if an email address has already been submited. The == would let double email entries into the database. Here is the code that would let double entries in. $query = "SELECT user_id from USERS where email='$e'"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { $query = "INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('$fn', '$ln', '$e', SHA('p'), NOW() )"; $result = @mysql_query ($query); if ($result) { echo '<h1 id="mainhead">Thank you!</h1> <p>You are now registered.</p><p><br /></p>'; include ('./includes/footer.inc'); exit(); } } else { echo '<h1 id="mainhead">Error!</h1> <p class="error">The email address has already been registered.</p>'; } Now here's what I changed it to and it stopped letting duplicate email addresses register. All I did was change == to != and switch the else statment with the if statement. Here it is. $query = "SELECT user_id from USERS where email='$e'"; $result = mysql_query($query); if (mysql_num_rows($result) != 0) { $query = "INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('$fn', '$ln', '$e', SHA('p'), NOW() )"; $result = @mysql_query ($query); if ($result) { echo '<h1 id="mainhead">Error!</h1> <p class="error">The email address has already been registered.</p>'; } } else { echo '<h1 id="mainhead">Thank you!</h1> <p>You are now registered.</p><p><br /></p>'; include ('./includes/footer.inc'); exit(); } Can someone please explain to me why == wouldn't work but != did? I can't understand this. Thank you much! Link to comment https://forums.phpfreaks.com/topic/122073-worked-for-this-but-didnt-can-someone-please-explain/ Share on other sites More sharing options...
cooldude832 Posted August 31, 2008 Share Posted August 31, 2008 the != says if the mysql_num_rows does not equal 0 so if it finds a match it doesn't equal 0 it is >0 so it fails just a fyi error check your queries and dont' supreme errors using the @ sign. Link to comment https://forums.phpfreaks.com/topic/122073-worked-for-this-but-didnt-can-someone-please-explain/#findComment-630176 Share on other sites More sharing options...
Benjigga Posted August 31, 2008 Author Share Posted August 31, 2008 Thanks for the heads up. I understand != means Not Equal To and == means Equal To, I just don't understand why in this situation Not Equal To worked and Equal To didn't? Link to comment https://forums.phpfreaks.com/topic/122073-worked-for-this-but-didnt-can-someone-please-explain/#findComment-630183 Share on other sites More sharing options...
cooldude832 Posted August 31, 2008 Share Posted August 31, 2008 only answer I can see from it is that because you might have defined the email field to be unique so when it tries to insert it on a duplicate value you get a query error you are suppressing. Link to comment https://forums.phpfreaks.com/topic/122073-worked-for-this-but-didnt-can-someone-please-explain/#findComment-630188 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2008 Share Posted August 31, 2008 Thanks for the heads up. I understand != means Not Equal To and == means Equal To, I just don't understand why in this situation Not Equal To worked and Equal To didn't? But if it works if you set ==, then obviously it won't work with !=. Something can't be both == and != simultaneously. $query = "SELECT user_id from USERS where email='$e'"; $result = mysql_query($query); if (mysql_num_rows($result) != 0) { echo '<h1 id="mainhead">Thank you!</h1> <p>You are now registered.</p><p><br /></p>'; include ('./includes/footer.inc'); exit(); } else { $query = "INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('$fn', '$ln', '$e', SHA('p'), NOW() )"; $result = @mysql_query ($query); if ($result) { echo '<h1 id="mainhead">Error!</h1> <p class="error">The email address has already been registered.</p>'; } } Link to comment https://forums.phpfreaks.com/topic/122073-worked-for-this-but-didnt-can-someone-please-explain/#findComment-630195 Share on other sites More sharing options...
Benjigga Posted August 31, 2008 Author Share Posted August 31, 2008 But if it works if you set ==, then obviously it won't work with !=. Something can't be both == and != simultaneously. Example1: if (sql == 0) { Do 1 } else { Do 2 } In the first code, it was practically like the example above. In the second code, where I changed == to != I changed it to: Example2: if (sql != 0) { Do 2 } else { Do 1 } Understand? It worked with example 2, but not example 1. And I was just wondering if anyone could explain why? Link to comment https://forums.phpfreaks.com/topic/122073-worked-for-this-but-didnt-can-someone-please-explain/#findComment-630203 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2008 Share Posted August 31, 2008 But if it works if you set ==, then obviously it won't work with !=. Something can't be both == and != simultaneously. Example1: if (sql == 0) { Do 1 } else { Do 2 } In the first code, it was practically like the example above. In the second code, where I changed == to != I changed it to: Example2: if (sql != 0) { Do 2 } else { Do 1 } Understand? It worked with example 2, but not example 1. And I was just wondering if anyone could explain why? Yes, but I don't see the change. Both codes look the same except you change == to !=. You didn't alternate "Do 1" and "Do 2". Link to comment https://forums.phpfreaks.com/topic/122073-worked-for-this-but-didnt-can-someone-please-explain/#findComment-630208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.