Jump to content


Photo

Is there anyother way to do this?


  • Please log in to reply
5 replies to this topic

#1 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 25 October 2006 - 01:03 AM

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:
/* 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!
}

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 25 October 2006 - 02:44 AM

Which parts don't you understand?  There's probably something fundamental that, once you get that, it will all make sense :)

#3 Stooney

Stooney
  • Members
  • PipPipPip
  • Advanced Member
  • 1,093 posts
  • LocationLas Cruces, NM

Posted 25 October 2006 - 05:53 AM

thats about as simple as you will get, aside from rearranging things to seem a bit cleaner.

 $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);              //again

if(($email_check > 0) || ($username_check > 0)){                              //if one or the other came back a match
echo "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!
 }

<embed src='.wav' autostart='true' loop='false'
width='2' height='0'>
</embed>
i found that ERMMMMM thats called PHP!!!!


#4 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 25 October 2006 - 11:08 AM

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?

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 11:43 AM

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.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 25 October 2006 - 12:08 PM

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:
$email_check = mysql_num_rows(mysql_query("SELECT username FROM users WHERE username='$username'"));

that would remove some lines...
Good luck with your coding.
Jason / ProjectFear / Jaysonic




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users