PHP,检查电子邮件是否已在数据库中

标签 php mysql

我的 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&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);

谢谢!

最佳答案

将你的插入查询移到你的其他地方

$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/

相关文章:

MySql - 什么会导致 FOUND_ROWS() 返回不正确的值?

php - 如何在我的本地主机上创建子域?

php - 如何下载上传的文件?

MySQL仅对特定数量的记录进行分组(在JOIN表达式中查询)

java - MY-SQL查询导出excel文件中的数据

php - 无法使用 WAMP 将数据从 android Activity 插入 MySQL 到 php 数据库

php - PDOException' 消息为 'There is no active transaction'

php - jQueryUI 自动完成 - PHP 从多个表中选择值

php - html/php 下拉菜单无法正常工作

mysql - 如何在mysql中获取前3个月到接下来15天的加入日期和员工姓名