PHP表单不发送到MYSQL数据库

时间:2017-10-31 14:51:28

标签: php mysql database

我目前正在尝试获取一个表单,以便将人们填写的数据发送到MYSQL数据库。在添加此代码之前,表单已正常运行,它将信息输出到我的电子邮件中。

这里是connection.php:

Element

以及这里的完整speaker.php代码:

<?php

function Connect()
{
 $dbhost = "localhost:8889";
 $dbuser = "******";
 $dbpass = "******";
 $dbname = "sfus18_speakers";

 $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);

 return $conn;
}

?>

知道我做错了什么吗?

编辑1:这是表格(speakerapp.php),它很长。在我尝试将数据发送到MySQL之前,表单已正常运行。

    <?php



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



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "xxx";

    $email_subject = "yyy";





    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['fName']) ||

        !isset($_POST['lName']) ||

        !isset($_POST['email']) ||

        !isset($_POST['cell']) ||

        !isset($_POST['company']) ||

        !isset($_POST['title']) ||

        !isset($_POST['address']) ||

        !isset($_POST['address2']) ||

        !isset($_POST['city']) ||

        !isset($_POST['state']) ||

        !isset($_POST['zip']) ||

        !isset($_POST['shirt']) ||


        !isset($_POST['bio']) ||

        !isset($_POST['preTitle']) ||

        !isset($_POST['subTitle']) ||

        !isset($_POST['format']) ||

        !isset($_POST['abstract']) ||

        !isset($_POST['audience']) ||

        !isset($_POST['additional']))

    {

        died('We are sorry, but there appears to be a problem with the form you submitted.');       

    }



    $fName = $conn->mysqli_real_escape_string ($_POST['fName']); // required

    $lName = $conn->mysqli_real_escape_string ($_POST['lName']); // required

    $email = $conn->mysqli_real_escape_string ($_POST['email']); // required

    $cell = $conn->mysqli_real_escape_string ($_POST['cell']); // required

    $company = $conn->mysqli_real_escape_string ($_POST['company']); // required

    $title = $conn->mysqli_real_escape_string ($_POST['title']); // required

    $address = $conn->mysqli_real_escape_string ($_POST['address']); // required

    $address2 = $conn->mysqli_real_escape_string ($_POST['address2']); // not required

    $city = $conn->mysqli_real_escape_string ($_POST['city']); // required

    $state = $conn->mysqli_real_escape_string ($_POST['state']); // required

    $zip = $conn->mysqli_real_escape_string ($_POST['zip']); // required

    $shirt = $conn->mysqli_real_escape_string ($_POST['shirt']); // required



    $bio = $conn->mysqli_real_escape_string ($_POST['bio']); // required

    $preTitle = $conn->mysqli_real_escape_string ($_POST['preTitle']); // required

    $subTitle = $conn->mysqli_real_escape_string ($_POST['subTitle']); // not required

    $format = $conn->mysqli_real_escape_string ($_POST['format']); // required

    $abstract = $conn->mysqli_real_escape_string ($_POST['abstract']); // required

    $audience = $conn->mysqli_real_escape_string ($_POST['audience']); // required

    $additional = $conn->mysqli_real_escape_string ($_POST['additional']); // not required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$fName)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$lName)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/";

  if(preg_match($string_exp,$cell)) {

    $error_message .= 'The cell phone number you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$company)) {

    $error_message .= 'The company you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$title)) {

    $error_message .= 'The job title you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$address)) {

    $error_message .= 'The address you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$city)) {

    $error_message .= 'The city you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$state)) {

    $error_message .= 'The state/province you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$zip)) {

    $error_message .= 'The postal code you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$shirt)) {

    $error_message .= 'The shirt size you entered does not appear to be valid.<br />';

  }

  if(preg_match($string_exp,$bio)) {

    $error_message .= 'Please enter your biography.<br />';

  }

   if(preg_match($string_exp,$preTitle)) {

    $error_message .= 'Please enter the title of your presentation.<br />';

  }

   if(preg_match($string_exp,$format)) {

    $error_message .= 'Please enter the format of your presentation.<br />';

  }

   if(preg_match($string_exp,$abstract)) {

    $error_message .= 'Please enter the abstract.<br />';

  }

   if(preg_match($string_exp,$audience)) {

    $error_message .= 'Please enter the audience level of your presentation.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }




    $email_message .= "First Name: ".clean_string($fName)."\n";

    $email_message .= "Last Name: ".clean_string($lName)."\n";

    $email_message .= "Email: ".clean_string($email)."\n";

    $email_message .= "Cell Phone: ".clean_string($cell)."\n";

    $email_message .= "Company: ".clean_string($company)."\n";

    $email_message .= "Title: ".clean_string($title)."\n";

    $email_message .= "Address: ".clean_string($address)."\n";

    $email_message .= "Address2: ".clean_string($address2)."\n";

    $email_message .= "City: ".clean_string($city)."\n";

    $email_message .= "State: ".clean_string($state)."\n";

    $email_message .= "Zip: ".clean_string($zip)."\n";

    $email_message .= "Shirt Size: ".clean_string($shirt)."\n";

    $email_message .= "Bio: ".clean_string($bio)."\n";

    $email_message .= "Presentation Title: ".clean_string($preTitle)."\n";

    $email_message .= "Subtitle: ".clean_string($subTitle)."\n";

    $email_message .= "Format: ".clean_string($format)."\n";

    $email_message .= "Abstract: ".clean_string($abstract)."\n";

    $email_message .= "Audience Level: ".clean_string($audience)."\n";

    $email_message .= "Additional Information: ".clean_string($additional)."\n";

// create email headers

$headers = 'From: xxx'.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  


?>

<?php
}
extract($_POST);

echo "<pre>";

print_r($_POST);

$query = "INSERT into tb_cform (fName, lName, email, cell, company, title, address, address2, city, state, zip, shirt, bio, preTitle, subTitle, format, abstract, audience, additional) VALUES ('" . $fName . "', '" . $lName . "', '" . $email . "', '" . $cell . "', '" . $company . "', '" . $title . "', '" . $address . "', '" . $address2 . "', '" . $city . "', '" . $state . "', '" . $zip . "', '" . $shirt . "', '" . $bio . "', '" . $preTitle . "', '" . $subTitle . "', '" . $format . "', '" . $abstract . "', '" . $audience . "', '" . $additional . "',)";

print_r($query);

?>     

1 个答案:

答案 0 :(得分:1)

删除最后&#34;,&#34;在查询中

<?php
$query   = "INSERT into tb_cform (fName, lName, email, cell, company, title, address, address2, city, state, zip, shirt, bio, preTitle, subTitle, format, abstract, audience, additional) VALUES ('".$fName."', '".$lName."', '".$email."', '".$cell."', '".$company."', '".$title."', '".$address."', '".$address2."', '".$city."', '".$state."', '".$zip."', '".$shirt."', '".$bio."', '".$preTitle."', '".$subTitle."', '".$format."', '".$abstract."', '".$audience."', '".$additional."')";
?>