I wrote some codes for the form to check to see if username and email address is not taken then allow to it to upload to sql db. Everything seems fine until I decided to test by putting different username with same email address. It seems the function didnt run properly as I want it to be. The bug shows the result: "Your Information is submitted! Thank you! Your Email address is either blank or taken!" I dont know how to force it to recheck before send "Your Information is submitted! Thank You!". Here is my code:
<?
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$first=$_POST['first'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
$query = mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE first = '" . $first . "'" ) and mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE email = '" . $email . "'" );
$row = mysql_fetch_object( $query ) or die(mysql_error());
if( $row->cnt == 0 )
{
print 'Your Information is submitted! Thank you!';
$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
}
else
{
print 'Your Information cant submit because of...';
}
$query = mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE first = '" . $first . "'" ) or die(mysql_error());
$row = mysql_fetch_object( $query ) or die(mysql_error());
if( $row->cnt == 0 )
{
$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
}
else
{
echo '<br>Your username is either blank or taken!';
}
$query = mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE email = '" . $email . "'" ) or die(mysql_error());
$row = mysql_fetch_object( $query ) or die(mysql_error());
if( $row->cnt == 0 )
{
$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
}
else
{
print '<br>Your email address is either blank or taken!';
}
mysql_query($query);
mysql_close();
?>
Also of course I already set email and first as unique. Any help will be appreciated. Thanks.
Darthvazor