Jump to content

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!
}
Link to comment
https://forums.phpfreaks.com/topic/25006-is-there-anyother-way-to-do-this/
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]
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?
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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.