jwk811 Posted October 25, 2006 Share Posted October 25, 2006 Trying to check if some information is already being used in a database. (On a membership thing) Is there another way to do this, maybe something a little easier that I could understand? Heres the part in the script that checks if the info is already being used:[code][/code]/* Let's do some checking and ensure that the user's email address or username does not exist in the database */ $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'"); $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(($email_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 different Email address!<br />"; unset($email_address); } if($username_check > 0){ echo "The username you have selected has already been used by another member in our database. Please choose a different Username!<br />"; unset($username); } include 'join_form.html'; // Show the form again! exit(); // exit the script so that we do not create this account! } Quote Link to comment https://forums.phpfreaks.com/topic/25006-is-there-anyother-way-to-do-this/ Share on other sites More sharing options...
btherl Posted October 25, 2006 Share Posted October 25, 2006 Which parts don't you understand? There's probably something fundamental that, once you get that, it will all make sense :) Quote Link to comment https://forums.phpfreaks.com/topic/25006-is-there-anyother-way-to-do-this/#findComment-113988 Share on other sites More sharing options...
Stooney Posted October 25, 2006 Share Posted October 25, 2006 thats about as simple as you will get, aside from rearranging things to seem a bit cleaner.[code] $sql_email_check = mysql_query("SELECT email_address FROM users //checks for stored emails matching the one is question WHERE email_address='$email_address'"); $sql_username_check = mysql_query("SELECT username FROM users //same only with usernames WHERE username='$username'"); $email_check = mysql_num_rows($sql_email_check); //puts the results in a readable variable $username_check = mysql_num_rows($sql_username_check); //againif(($email_check > 0) || ($username_check > 0)){ //if one or the other came back a matchecho "Please fix the following errors: //state a problem"; if($email_check > 0){ //if problem was email, say so echo "<strong>Your email address has already been used by another member in our database. Please submit a different Email address!"; unset($email_address); //unset email variable (not exactly nessesary) } if($username_check > 0){ //if problem was username, say so echo "The username you have selected has already been used by another member in our database. Please choose a different Username!"; unset($username); //again not really needed } include 'join_form.html'; // Show the form again! exit(); // exit the script so that we do not create this account! }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25006-is-there-anyother-way-to-do-this/#findComment-114025 Share on other sites More sharing options...
jwk811 Posted October 25, 2006 Author Share Posted October 25, 2006 when it does this $email_check = mysql_num_rows($sql_email_check); $username_check = mysql_num_rows($sql_username_check);its changing the result of the checking into how many rows in the database had that username and email? Quote Link to comment https://forums.phpfreaks.com/topic/25006-is-there-anyother-way-to-do-this/#findComment-114125 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 That's correct, if the username and email don't exist then the number of rows returned will be zero. If they do exist then the number of rows will be greater than zero.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25006-is-there-anyother-way-to-do-this/#findComment-114141 Share on other sites More sharing options...
JasonLewis Posted October 25, 2006 Share Posted October 25, 2006 chrisdburns you dont need all of those useless comments in there. you could make it a tad smaller by just skipping the $sql_email_check vars and going straight to the $email_check var. example:[code=php:0]$email_check = mysql_num_rows(mysql_query("SELECT username FROM users WHERE username='$username'"));[/code]that would remove some lines... Quote Link to comment https://forums.phpfreaks.com/topic/25006-is-there-anyother-way-to-do-this/#findComment-114149 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.