PHP MySQLi电子邮件表单

时间:2013-10-25 14:31:39

标签: php forms email web mysqli

我需要通过让大家知道我正处于学习网络编程的最初阶段。我完全理解阅读代码并知道它在做什么。我正在学习使用课程,在线培训和“现实世界”培训。

我正在尝试创建的联系表单存在问题。我工作的公司有几个使用联系表格的网站。我使用了在其他网站上工作的联系表单代码,但是我们已经从这个新主机开始更改了主机,他们使用MySQLi而不是MySQL。当我使用以下代码时,它会发布到数据库并按预期工作。

<?php
//hake yachts contact

//connection

$con =   mysqli_connect("localhost","hakeyachtcontact","hakeyachtcontact","hakeyachtcontact");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//insert
$sql="INSERT INTO hakeyachtcontact (firstName, lastName, email,  boatModel)
VALUES
('$_POST[firstName]','$_POST[lastName]','$_POST[email]','$_POST[boatModel]')";

if (!mysqli_query($con,$sql))
  {
    die('Error: ' . mysqli_error($con));
  }

mysqli_close($con);

 ?>

现在......当我添加以下代码时,电子邮件不会发送,以前的功能(将数据发布到数据库)停止工作。

//email
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "joshua.delph@heartlandfpg.com";
    $email_subject = "Hake Yachts Website Contact";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['firstName']) ||
       !isset($_POST['lastName']) ||
       !isset($_POST['email']) ||
       !isset($_POST['boatModel'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

$firstName = $_POST['firstName']; // required
$lastName = $_POST['lastName'];// required
$email = $_POST['email'];// required
$boatModel = $_POST['boatModel'];// required


    $error_message = "";
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){ 
   $error_message .= 'The email you entered does not appear to be valid.<br />';
  }
   $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$firstName)) {
    $error_message .= 'The name you entered does not appear to be valid.<br />';
  }
  $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$lastName)) {
    $error_message .= 'The name you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($firstName)."\n";
    $email_message .= "email: ".clean_string($lastName)."\n";
    $email_message .= "phone: ".clean_string($email)."\n";
    $email_message .= "comments: ".clean_string($boatModel)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

//send
header("Location: thankyou.html");

我有一种潜在的怀疑,这是由于MySQL和MySQLi之间的差异,但我不相信是这样的。我使用谷歌进行了搜索,但没有找到任何有助于此的内容,我尝试了几种不同的解决方案,我在网上找到了。我真的很想弄明白这一点。

我也对通过电子邮件提交网络表单的其他解决方案持开放态度。

我真的很感激任何人都可以提供帮助。我愿意通过教程或其他任何有助于我学习的内容。

谢谢!

2 个答案:

答案 0 :(得分:0)

变化:

$sql="INSERT INTO hakeyachtcontact (firstName, lastName, email,  boatModel)
VALUES
('$_POST[firstName]','$_POST[lastName]','$_POST[email]','$_POST[boatModel]')";

if (!mysqli_query($con,$sql))
  {
    die('Error: ' . mysqli_error($con));
  }

为:

$firstname = $con->mysqli_real_escape_string($_POST['firstName']);
$lastname = $con->mysqli_real_escape_string($_POST['lastName']);
$email = $con->mysqli_real_escape_string($_POST['email']);
$boatmodel = $con->mysqli_real_escape_string($_POST['boatModel']);

$sql="INSERT INTO hakeyachtcontact (firstName, lastName, email,  boatModel)
VALUES
('$firstname','$lastname','$email','$boatmodel')";

if ($con->query($sql) === FALSE)
  {
    die('Error: ' . mysqli_error($con));
  }

答案 1 :(得分:0)

if(isset($_POST['email'])) {上缺少右括号。只需将它添加到需要关闭支票的地方。