Checking if email already exists in database

Hello,
I’m having some problems… I want the code to check if the submitted email address already exists in the database. If not, then it should add the email to the database.
The problem is that it doesn’t matter which email address I submit, it will still output the error message saying “email is already registered”. Even with an email I know doesn’t exists in the database, it will still output the same error.

Any help is appreciated.

[php]

<?php if(empty($_POST) === false) { $errors = array(); $email = $_POST['email']; $emailcheck = mysql_query("SELECT * FROM `rss`(`email`) WHERE `email` = '$email'"); if(empty($email) === true) { $errors[] = 'An email adress is required.'; } if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) { $errors[] = 'Invalid email adress.'; } else if($emailcheck === false) { $errors[] = 'This email is already registered.'; } else { mysql_query("INSERT INTO `rss`(`email`) VALUE ('$email')"); } } ?>

[/php]

try this
[php]
$emailcheck = mysql_query(“SELECT * FROM rss(email) WHERE email = ‘$email’”) or die(mysql_error());
[/php]

if query gives you error fix it.

also try using == operator and see if it works

thanks, I did get an error report… a syntax error that I fixed.

Right now I can register new email addresses as long as they don’t already exist. Problem is that it doesn’t display the message saying “This email is already registered.” when I enter an address that has been registered.

[php]

<?php if(empty($_POST) === false) { $errors = array(); $email = $_POST['email']; $emailcheck = mysql_query("SELECT * FROM `rss` WHERE `email` = '$email'"); if(empty($email) === true) { $errors[] = 'An email adress is required.'; } if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) { $errors[] = 'Invalid email adress.'; } else if($emailcheck === false) { $errors[] = 'This email is already registered.'; } else { mysql_query("INSERT INTO `rss`(`email`) VALUE ('$email')"); } } ?>

[/php]

ok i made some changes to your codes

[php]

<?php if(empty($_POST) === false) { $errors = array(); $email = $_POST['email']; $emailcheck = mysql_query("SELECT * FROM `rss` WHERE `email` = '$email'"); $row = mysql_fetch_assoc($emailcheck); if(empty($email) === true) { $errors[] = 'An email adress is required.'; } if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) { $errors[] = 'Invalid email adress.'; } elseif($row['email'] == $email) { $errors[] = 'This email is already registered.'; } else { mysql_query("INSERT INTO `rss`(`email`) VALUE ('$email')"); } } ?>

[/php]

wow! thanks a lot, works like a charm!

Do you know why it didn’t work with the previous code?
Shouldn’t the mysql_query() have returned FALSE if the email already existed?

strange…? :slight_smile:

no the query will only return false if it fails, if the query succeed u will have true
but its only a boolean value if you need to compare with something to get the value from the query using MYSQL_FETCH_ASSOC($RESULT); and then compare it with whatever you wanna compare it to

ahh ok I understand…

thanks a ton!!!

mysql_query() doesnt do any logic it just fetch what ever you specify on SQL query

glad that i could help

solved!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service