gpt4 book ai didi

php - 如何避免重复的电子邮件 php/sql?

转载 作者:行者123 更新时间:2023-11-28 23:11:39 26 4
gpt4 key购买 nike

我使用了这段代码,但我不知道问题出在哪里,我也使用了不同的代码

我想做什么来检查并且不允许用户两次添加他的电子邮件

<?php
include("includedb.php");
//declare variables
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$gift = $_POST['gift'];
$formName = $_POST['formName'];
$formEmail = $_POST['formEmail'];
$formEmirate = $_POST['formEmirate'];
$birthday = $_POST['birthday'];
$date = $_POST['date'];

$result = mysqli_query("SELECT * FROM users WHERE email = '$email'") or exit(mysqli_error()); //check for duplicates

$num_rows = mysqli_num_rows($result); //number of rows where duplicates exist

if ($num_rows == 0) { //if there are no duplicates...insert
$sql = "INSERT INTO users (name, email, tel, gift, formName, formEmail, formEmirate, birthday, date)
VALUES ('$name', '$email', '$tel','$gift', '$formName', '$formEmail', '$formEmirate','$birthday',CURRENT_TIMESTAMP )";
if (!mysqli_query($sql)) {
die('Error: ' . mysqli_error());
}
}

mysqli_close();

header("location: thank-you.html?remarks=success");

?>

最佳答案

问题是你没有将任何连接传递给 mysql_query

因此查询不会被查询

$conn = your connection;

$result = mysqli_query($conn,"SELECT * FROM users WHERE email = '$email'") or exit(mysqli_error()); //check for duplicates
$num_rows = mysqli_num_rows($result); //number of rows where duplicates exist
if($num_rows == 0) { //if there are no duplicates...insert
$sql = "INSERT INTO users (name, email, tel, gift, formName, formEmail, formEmirate, birthday, date)
VALUES ('$name', '$email', '$tel','$gift', '$formName', '$formEmail', '$formEmirate','$birthday',CURRENT_TIMESTAMP )";
if (!mysqli_query($conn,$sql))
{
die('Error: ' . mysqli_error());
}
}

关于php - 如何避免重复的电子邮件 php/sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45709437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com