...

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



jj24jj
03-10-2010, 04:13 AM
Hi,

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?

Thanks

-------------------------------



<?php
//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";
$flag1="OK";
}
else{
$emailclass = "errortext";
$flag1="NOTOK";
$emailvalidationcheck ="NOTOK";
}
?>


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

<?php

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);
mysql_free_result($result);
if(isset($result[customers_email_address])){
$emailclass = "errortext";
$flag="NOTOK";
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";
$flag="OK";
}
}

?>

_Aerospace_Eng_
03-10-2010, 04: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.

jj24jj
03-10-2010, 04: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum