简体   繁体   English

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

[英]PHP skips check and insert data even if it already exists

I have having a difficult time trying to determine if an email exists in my database table. 我很难确定数据库表中是否存在电子邮件。 Currently, it seems to skip the check and insert the email data to the db. 当前,似乎跳过了检查并将电子邮件数据插入数据库。 It does this even if the email exists in the database. 即使电子邮件存在于数据库中,它也会这样做。

Any ideas on what I'm doing wrong? 关于我在做什么错的任何想法吗? I've tried a number of ways, but so far all of them "ignore" the verification step and inserts the duplicate data 我已经尝试了多种方法,但是到目前为止,所有这些方法都“忽略”了验证步骤并插入了重复数据

Here is my code: 这是我的代码:

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

The mysqli_query you are using to check if the email exists needs a reference to the mysql connection. 您用于检查电子邮件是否存在的mysqli_query需要引用mysql连接。

Change this line: 更改此行:

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

to this: 对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM