我正在试图弄清楚如何检查电子邮件是否已经存在于数据库中,因为我不想复制我尝试使用Google搜索,而且我认为它没有用作旧帖子?
这是我的代码:
$emailcheck = $value2;
$emailcheck = "SELECT email FROM demo WHERE email= '$emailcheck' ";
$result = mysql_query($link, $emailcheck);
$count = mysql_num_rows($result);
if ($count > 0){
{$_SESSION['email'] = "Email is already used!";}
}
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";} else {
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{$_SESSION['email'] = "Email is incorect";}}
if(empty($_POST["name"]) || empty($_POST["email"]) || (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) || ($count > 0))
{
header("Location: home.php");
} else {
感谢任何人的帮助!
我几乎完整的代码:
$value = $_POST['name'];
$value2 = $_POST['email'];
$emailcheck = $value2;
$emailcheck = "SELECT email FROM demo WHERE email= '$emailcheck' ";
$result = mysql_query($link, $emailcheck);
$count = mysql_num_rows($result);
if ($count > 0){
{$_SESSION['email'] = "Email is already used!";}
}
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";} else {
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{$_SESSION['email'] = "Email is incorect";}}
if(empty($_POST["name"]) || empty($_POST["email"]) || (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) || ($count > 0))
{
header("Location: home.php");
} else {
if (!$link) {
die('Could not connect: ' . mysql_errno());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('can\'t use ' . DB_NAME . ':' . mysql_error());
}
$sql = "INSERT INTO demo (name, email) VALUES ('$value', '$value2')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
header("location: https://www.google.co.uk/search?q=Ghostbusters&stick=H4sIAAAAAAAAAONgFuLUz9U3MI43skxR4gIxjUxLcrLTtYQck0vyi0LyQ8Ic81J888syU4sBdwGq3i0AAAA&sa=X&ved=0ahUKEwj0-J6QtuXaAhUQSsAKHaYqB2wQxA0I6gEwHg&biw=1280&bih=699");
mysql_close();
session_destroy();
}
}
?>
答案 0 :(得分:0)
我认为你必须把
die;
之后
header("Location: home.php");
否则代码仍将继续执行
答案 1 :(得分:0)
Peshaw和Tin绝对正确;即使您发现重复的电子邮件,也无需改变或停止代码执行。
将知道是否存在重复,但您的代码将不会像当前所写的那样回复。
将结果存储在会话中后,您需要一些东西......
$isDuplicate = false;
if ($count > 0){
$_SESSION['email'] = "Email is already used!";
$isDuplicate = true;
}
if(empty($_POST["name"]) || empty($_POST["email"]) || (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) || ($count > 0) || ($isDuplicate)) {
// error handling here
header("Location: home.php");
} else {
// regular code execution here
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";} else {
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{$_SESSION['email'] = "Email is incorrect";}}
}
...当然,如果没有通常警告将用户输入直接插入查询而不是使用PDO对其进行参数化或至少消毒它们,这个答案就不会完整。请清理您的输入。
答案 2 :(得分:0)
您需要将if ($count > 0)
之后的其余代码包装在else
代码块中,如下所示:
if ($count > 0){
$_SESSION['email'] = "Email is already used!";
} else {
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
// the rest of your code
}