View Full Version : PhP/MySQL - Checking if an email exists in a database

03-10-2010, 05:13 AM

I am trying to get some code straight. I know this is a complete and utter mess, my apologies (newbie coder). The aim of the script is that if the email matches a regex expression (previously defined), then it checks the database to see if the email the user has entered is already there. If it is, it prints back a message telling the user to enter a new one.

The database field itself is unique, so technically there will be no other email that is the same, however for the sake of having to redirect the user back I'd like to just keep all the validation on one page.

However, when I enter an email that already exists in the database, it still lets me continue to the database page. Not sure why this is, can anyone help me out?



//Email Validation
$email = clean($_POST['Email1']);
$pattern = '/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-…
if ($emailpattern=(preg_match($pattern, $_POST['Email1'])) > 0){
$emailclass = "basictext";
$emailclass = "errortext";
$emailvalidationcheck ="NOTOK";

<td><div class="<?php print $emailclass; ?>">Email Address:*</div></td>
<td><input type="text" name="Email1" value="<?php print $email; ?>" size="30"/></td>


if ($emailvalidationcheck == "NOTOK"){
$flag = "NOTOK";
print '<tr><td align="center" colspan="2" class="errorsubtext"> Please enter a valid Email Address. </td></tr>';
else if ($emailvalidationcheck == "OK"){

$host="***********"; // Host name
$username="*********"; // Mysql username
$password= (DATABASE_PASSWORD); // Mysql password
$db_name="********"; // Database name
$tbl_name="customers"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = mysql_query( "SELECT `customers_email_address` FROM `".$tbl_name."` WHERE `customers_email_address` = \'" . mysql_real_escape_string( $email ) . "\' LIMIT 1" );
$result = mysql_fetch_row($sql);
$emailclass = "errortext";
print '<tr><td align="center" colspan="2" class="errorsubtext"> This email address is already in use. </td></tr>';
else if(!isset($result[customers_email_address])){
$emailclass = "basictext";


03-10-2010, 05:22 AM
Don't escape the single quotes in your query. Why did cut out parts of your code? The syntax highlighting is a little off.

03-10-2010, 05:23 AM
Sorry, that was just me being a nab, trying to figure out the forum layout :P should all just be in one place now. :o
Thanks for the suggestion, though unfortunately it is still not working. Not sure whats going on.