user4853107
user4853107

Reputation:

Prevent Email Duplicates

I have been trying to prevent any user from registering an email that is already registered in the database. I couldn't. Maybe it's because I have to check three tables at the same time. I tried but failed. I hope you are able to help me.

        //Check whether Email exists or not
        $check="SELECT * FROM admins, engineers, users WHERE admins.Email='$email' OR engineers.Email='$email' OR users.Email='$email'";
        $results=mysql_query($check);



        //Confirm that the Email doesn't exist
        if(mysql_num_rows($results) == 0)
        { $check='true'; }



        //Error message to the user that the Email already exists.
        else
        {
            header("location: register.php?email=false"); 
        }



        //Register the data
        if($check=='true')
        {
            mysql_query("INSERT INTO admins (Admin, Password, Email, Phone, Date, Time) VALUES ('$username', '$password','$email', '$phone', '$date', '$time')");
            header("location: login.php?register=success");
        }

Upvotes: 0

Views: 63

Answers (2)

mush
mush

Reputation: 165

You can make your life easier by storing all emails in one table. You can use a column named for example 'type' which would then be admin, user or engineer. This makes your lookups much easier instead of having to look through multiple tables.

Upvotes: 0

Barmar
Barmar

Reputation: 780889

Use UNION rather than a cross-product.

SELECT 1 FROM admins WHERE email = '$email'
UNION
SELECT 1 FROM engineers WHERE email = '$email'
UNION
SELECT 1 FROM users WHERE email = '$email'

The cross-product will only work if it finds matches in all the tables, not just one of them.

Upvotes: 1

Related Questions