我很难确定我的数据库表中是否存在电子邮件。目前,它似乎跳过检查并将电子邮件数据插入数据库。即使电子邮件存在于数据库中,它也会这样做。
关于我做错的任何想法?我尝试了很多方法,但到目前为止,所有这些方法都“忽略”了验证步骤并插入了重复的数据
这是我的代码:
<?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);
}
?>
答案 0 :(得分: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'");