gpt4 book ai didi

PHP 会跳过检查并插入数据,即使数据已经存在

转载 作者:行者123 更新时间:2023-11-30 01:11:42 24 4
gpt4 key购买 nike

我很难确定数据库表中是否存在电子邮件。目前,它似乎跳过检查并将电子邮件数据插入数据库。即使电子邮件存在于数据库中,它也会执行此操作。

关于我做错了什么有什么想法吗?我尝试了多种方法,但到目前为止,所有方法都“忽略”验证步骤并插入重复数据

这是我的代码:

 <?php
$con=mysqli_connect("localhost","usr1","123456%%","db1");
// Check conn
if (mysqli_connect_errno())
{
echo "Deu merda ao conectar ao MySQL: " . mysqli_connect_error();
}

//query to insert data to db
$sql="INSERT INTO workshops (nome, email, curso, telefone, semestre, workshop)
VALUES('$_POST[nome]','$_POST[email]','$_POST[curso]','$_POST[telefone]','$_POST[semestre]' ,'$_POST[workshop]')";

//stores email in a var
$email = $_POST['email'];

//query to look up for the email
$query = mysqli_query("SELECT `email` FROM `workshops` WHERE `email` = '$email'");

//runs $query to see if it finds any result
if(mysqli_num_rows($query) > 0) {
echo 'Email already registered'.mysqli_error();
mysqli_close($con);

} else {
mysqli_query($con,$sql);
echo "We received your request, thank you!";

//blah blah blah email in portuguese
$assunto = "Confirmaçãoao de inscrição";
$mensagem = "<h1>Obrigado!</h1><br>Recebemos sua inscrição no workshop" . $_POST['workshop'] . ". <br> Iremos entrar em contato em breve para confirmar sua presença.";
$from = "contato@mult13.com";
$headers = "From:". $from;
mail($_POST['email'], $assunto, $mensagem, $headers);
mysqli_close($con);
}
?>

最佳答案

您用来检查电子邮件是否存在的 mysqli_query 需要引用 mysql 连接。

更改此行:

//query to look up for the email
$query = mysqli_query("SELECT `email` FROM `workshops` WHERE `email` = '$email'");

对此:

//query to look up for the email
$query = mysqli_query($con, "SELECT `email` FROM `workshops` WHERE `email` = '$email'");

关于PHP 会跳过检查并插入数据,即使数据已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19416286/

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