0

我很难确定我的数据库表中是否存在电子邮件。目前,它似乎跳过了检查并将电子邮件数据插入数据库。即使电子邮件存在于数据库中,它也会这样做。

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

这是我的代码:

 <?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);
 }
 ?> 
4

1 回答 1

0

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'");
于 2013-10-17T00:40:11.743 回答