jskozerino
jskozerino

Reputation: 45

PHP, Checking if an Email is already in database

I am having troubles with my php code. What I have going on here is a form that gets sent to this file and enters it into a database. It does NOT enter into the database if the email is already in the database in an account. What I am having troubles with is I want to print out "And account with that email is already created" when that email is already in the database and print out "Account created" when it inserts the email into the database. What is wrong with it is no matter what email I enter when creating a new account, it runs the if statement even if I enter an email that is new the database. Just to be clear, the database will enter the email if it is a new email however my code will just automatically use the if statement.

Here is my code in php:

$fName=$_POST['fName'];
$lName=$_POST['lName'];
$email=$_POST['email'];
$password=$_POST['password'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$address1=$_POST['address1'];

Print"<h3>";
Print"<a href ='projectHome.php'>Store Home&nbsp;&nbsp;&nbsp;</a>";
Print"<a href ='createAccount.php'>Create an Account&nbsp;&nbsp;&nbsp;   </a>";
Print"<a href ='login.php'>Login&nbsp;&nbsp;&nbsp;</a>";
Print"<a href='BrowseCategories.php'>Browse Categories&nbsp;&nbsp;&nbsp; </a>";
Print"<a href='search.php'>Search for a book&nbsp;&nbsp;&nbsp; </a>";
Print"<a href='orders.php'>Orders&nbsp;&nbsp;&nbsp; </a>";
Print"<a href ='contactUs.php'>Contact Us&nbsp;&nbsp;&nbsp;</a>";
Print"<a href ='index.html'>Home(index)&nbsp;&nbsp;&nbsp;</a>";
Print"</h3>";


$query="insert into Customers(Email,Passwd,FirstName,LastName,Address1,ZipCode,State) VALUES ('$email','$password','$fName','$lName','$address1',$zip,'$state')";
mysql_query($query);

$query="select * from Customers where Email = '$email'";
$result=mysql_query($query);
$numOfRows=mysql_numrows($result);
if($numOfRows==1)
{
    print"An account with that email is already created..<a href ='createAccount.php'>Please enter a new account email.</a>";
    print"<a href ='createAccount.php'></a>";

}
else
{
    print"Account created.";
}


@mysql_close($connection);

Thank you!

Upvotes: 1

Views: 3408

Answers (1)

kenken
kenken

Reputation: 61

move your insert query to your else

$query="select * from Customers where Email = '$email'";
$result=mysql_query($query);
$numOfRows=mysql_num_rows($result);
if($numOfRows==1)
{
print"An account with that email is already created..<a href ='createAccount.php'>Please enter a new account email.</a>";
print"<a href ='createAccount.php'></a>";

}
else
{
$query="insert into Customers(Email,Passwd,FirstName,LastName,Address1,ZipCode,State) VALUES ('$email','$password','$fName','$lName','$address1',$zip,'$state')";
mysql_query($query);
print"Account created.";
}

Upvotes: 1

Related Questions