Jump to content

Archived

This topic is now archived and is closed to further replies.

jwk811

Is there anyother way to do this?

Recommended Posts

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!
}

Share this post


Link to post
Share on other sites
Which parts don't you understand?  There's probably something fundamental that, once you get that, it will all make sense :)

Share this post


Link to post
Share on other sites
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);              //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!
}[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.