php html表单有效电子邮件检查和发布

php html表单有效电子邮件检查和发布,php,Php,我正在创建PHP联系人表单验证脚本我的脚本电子邮件文件错误消息显示,但它正在向数据库插入无效电子邮件我只想插入有效电子邮件 这是我的密码 看起来您的代码正在执行以下操作: if POST if no input name error message else name = input name if no input email error message else email = input email if no input message

我正在创建PHP联系人表单验证脚本我的脚本电子邮件文件错误消息显示,但它正在向数据库插入无效电子邮件我只想插入有效电子邮件

这是我的密码


看起来您的代码正在执行以下操作:

if POST
  if no input name
    error message
  else
    name = input name
  if no input email
    error message
  else
    email = input email
  if no input message
    error message
  else
    message = input message

  insert name/email/message as a new entry
即使在if/else语句中设置了错误消息,最终还是会将记录插入数据库。在决定是否插入记录之前,您需要以某种方式检查错误。用类似的代码替换代码的最后一部分可能会得到您想要的结果:

if ( $nameErr != "" && $emailErr != "" && $messageErr != "" ) {
  $stmt = $con->prepare("INSERT INTO contact(name,email,message,id) VALUES (?,?,?,?)"); 
  $stmt->bind_param("ssss", $name,$email,$message,$id);
  if ($stmt->execute()) {
    echo "Your project added successfully";
  } else {
    echo "Failed to add your project";
  }
} else {
  echo "Failed to add your project";
}

嗯,您正在进行验证,但即使验证失败,您也会继续进行并添加到数据库中。您真的没有缩进代码,还是这是SO输入系统的副作用?您可以使用filter_var验证电子邮件。我添加了相同的结果