我的 PHP 代码有问题。我在这里进行的是发送到此文件并将其输入数据库的表单。如果电子邮件已经在帐户的数据库中,它不会进入数据库。我遇到的麻烦是,当该电子邮件已经在数据库中时,我想打印出“并且已经创建了该电子邮件的帐户”,并在将电子邮件插入数据库时打印出“已创建的帐户”。它的问题在于,无论我在创建新帐户时输入什么电子邮件,它都会运行 if 语句,即使我输入的电子邮件是数据库中的新电子邮件。需要说明的是,如果是新电子邮件,数据库将输入该电子邮件,但我的代码将自动使用 if 语句。
这是我在 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 </a>";
Print"<a href ='createAccount.php'>Create an Account </a>";
Print"<a href ='login.php'>Login </a>";
Print"<a href='BrowseCategories.php'>Browse Categories </a>";
Print"<a href='search.php'>Search for a book </a>";
Print"<a href='orders.php'>Orders </a>";
Print"<a href ='contactUs.php'>Contact Us </a>";
Print"<a href ='index.html'>Home(index) </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);
谢谢!
最佳答案
将你的插入查询移到你的其他地方
$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.";
}
关于PHP,检查电子邮件是否已在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933469/