简体   繁体   中英

PHP MySQLi Email Form

I need to being by letting you all know that I am at the very beginning stages of learning web programming. I understand enough to read the code and know what it is doing. I am learning using courses, online training, and "real world" training.

I am having an issue with a contact form I am attempting to create. The company I work for has several websites that use contact forms. I used the contact form code that has been working on the other website, but we have changed our host beginning with this new host and they use MySQLi rather than MySQL. When I use the following code, it posts to the database and works as expected.

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

 ?>

Now...when I add the following code, an email doesn't send and the previous functionality that worked (posting data to the database) stops working.

//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");

I have a sneaking suspicion that this is due to the differences between MySQL and MySQLi, but I am not confident that is the case. I have searched using Google and haven't found anything that has helped up to this point and I have tried several different solutions I found online. I am really at loss trying to figure this out.

I am also open to other solutions for submitting web forms via email.

I truly appreciate any help anyone can provide. I am willing to go through tutorials or anything else that would help me learn.

Thanks!

Change:

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

to:

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

There is a missing closing brace on if(isset($_POST['email'])) { . Just add it where you need to close off the check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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