1

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!

EternalHour
  • 8,308
  • 6
  • 38
  • 57
jskozerino
  • 45
  • 2
  • 6
  • 3
    your insert is happening **before** the select. switch that wagon with the horse ;-) – Funk Forty Niner Apr 29 '15 at 02:53
  • 2
    [*`mysql_num_rows()`*](http://php.net/manual/en/function.mysql-num-rows.php) not `mysql_numrows()` - you should take the opportunity here to upgrade to mysqli or PDO while you can as well – scrowler Apr 29 '15 at 02:54
  • 1
    you check after the insert, so there will always be that address in the db –  Apr 29 '15 at 02:54
  • 2
    oh lookie; our comments have been converted. vvvvvvvvvvvvvvvvvvvvv – Funk Forty Niner Apr 29 '15 at 02:57
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 29 '15 at 11:39

1 Answers1

1

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.";
}
kenken
  • 61
  • 7