PhP - MySQL validation error?

m3m9h1s

New member
Hi,

I got some pointers last night on how to improve a MySQL query I was trying to execute, and the idea is that I am trying to validate an email field. Firstly if the email does not match a regex expression the statement "Please enter a valid email address." is displayed. Secondly if it passes the regex test, a database query is called to check wether the email address is already in use, and if it is, the user gets the return message "This email address is already in use". The email field is unique so technically there wouldn't be another email with the same address, but I would still like to be able to display a message to the user if they enter an incorrect address and prompt them for a new one.

Although, the first part of validation checks out, the second part does not seem to be functioning as when I put in an email address already stored in the database, it lets me carry on to the next page. Can anyone tell me why this isnt working?

Thanks

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

<?php

require('Configure.php');
require('Filenames.php');
require('Cleanstring.php');
require(FILENAME_HEADER);
require('Info_Files/DB_Password.php');
require('RegisterFunctions.php');


$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");

?>

<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"){
$sql = "select count(*) from `".$tbl_name."` where `customer_email_address` = '" .mysql_real_escape_string($email). "'";
$list = mysql_query($sql);
$lst = mysql_fetch_array($list);
$count = $lst[0];
mysql_free_result($list);
if($count >= 1){
$emailclass = "errortext";
$flag="NOTOK";
print '<tr><td align="center" colspan="2" class="errorsubtext"> This email address is already in use. </td></tr>';
}
else if($count ==0){
$emailclass = "basictext";
$flag="OK";
}}

?>
 
Back
Top