简体   繁体   中英

Checking if e-mail address is already used

I'm coding a registration system on php. I'm trying to make it impossible to create two accounts with same e-mail address. I wrote a code which should check it, but it doesn't work. I can create as many accounts as I want with the same e-mail. What's wrong with it?

$sqlemail = "SELECT count(*) FROM 'users' WHERE email = ?"; 
$result = $connection->prepare($sqlemail); 
$result->execute($email); 
$emailused = $result->fetch();

if ($emailused!=false) {
    echo 'An account with this e-mail address already exists!';
}

Make the email field a unique key for the table. Then, if you try to insert a duplicate, mysql will throw an error.

You can then handle the error, and display your friendlier message. You can check for the error with $result->errorCode() .

This also keeps you from having to make a SELECT before an INSERT .

Can you try this, use backticks in users not '

 $sqlemail = "SELECT count(*) FROM `users` WHERE email = ?"; 

if ($emailused>0) {
    echo 'An account with this e-mail address already exists!';
}

Even if the email came back with multiple results, the script will execute because you are not killing the script! You need to use exit; or die() to stop them from progressing, or a re-direct or something.

if ($emailused>0) {
    echo 'An account with this e-mail address already exists!';
    exit;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM