获取Integrity Constraint Violation,当值不为null时

时间:2016-05-26 20:18:53

标签: php mysql pdo

我有一个表单,我从中插入数据,我希望它插入到我的mysql数据库中。它还会发送一封电子邮件。出于某种原因,当我在表单上填写姓名,电子邮件和状态输入框并提交时,它会说" name"不能为空,但我只是输入了我的名字!我的数据库表是这样的:

database structure

我在想,因为也许我没有为它丢弃的id插入任何内容,但为什么它会说name为null而不是id?

我的代码(整页):

<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){
  //variables to be used from each form field's input.
  $name = trim( filter_input( INPUT_POST, "name", FILTER_SANITIZE_STRING ) );
  $email = trim( filter_input( INPUT_POST, "email", FILTER_SANITIZE_EMAIL ) );
  $state = trim( filter_input( INPUT_POST, "state", FILTER_SANITIZE_STRING ) );

  //Blank fields cannot be submitted.
  if($name == "" || $email == "" || $state == ""){
    $error_message = 'Please fill in the required fields (Name, Email, State)';
  }

  //Honeypot for spam bots. if not blank, bad form input.
  if(!isset($error_message) && $_POST['details'] !== ""){
    $error_message = 'Bad form input!';
  }

  //Adding PHPMailer
  require( 'phpmailer/PHPMailerAutoload.php' );

  $mail = new PHPMailer;

  if(!isset($error_message) && !$mail->validateAddress($email)){
    $error_message = 'Invalid Email Address';
  }

  if(!isset($error_message)){
    //Creating the email body to be sent
    $email_body = "";
    $email_body .= "Name: " . $name . "\n";
    $email_body .= "Email: " . $email . "\n";
    $email_body .= "State: " . $state . "\n";

    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Host = "mail.me.net";
    $mail->Port = 587;
    $mail->Username = "me@me.net";
    $mail->Password = "password";
    //Sending the actual email
    $mail->setFrom($email, $name);
    $mail->addAddress('me@me.net', 'Me');     // Add a recipient
    $mail->isHTML(false);                                  // Set email format to HTML
    $mail->Subject = 'Calculation form results from ' . $email;
    $mail->Body = $email_body;

    if($mail->send()) {
      //show thank you message
      header('location:index.php?status=thanks');
      exit;
    }
    $error_message = 'Message could not be sent. ';
    $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;
  }

}
?>
<!DOCTYPE html>
<!--[if lte IE 6]><html class="preIE7 preIE8 preIE9"><![endif]-->
<!--[if IE 7]><html class="preIE8 preIE9"><![endif]-->
<!--[if IE 8]><html class="preIE9"><![endif]-->
<!--[if gte IE 9]><!--><html><!--<![endif]-->
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>title</title>
    <meta name="author" content="name">
    <meta name="description" content="description here">
    <meta name="keywords" content="keywords,here">
    <link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon">
    <link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
    <?php if(isset($_GET['status']) && $_GET['status'] == 'thanks') {
        echo '<p>Thanks! Your data has been sent!</p>';
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "calculations";

        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // prepare sql and bind parameters
            $stmt = $conn->prepare("INSERT INTO people (name, email, state) VALUES (:name, :email, :state)");
            $stmt->bindParam(':name', $name);
            $stmt->bindParam(':email', $email);
            $stmt->bindParam(':state', $state);
            echo $name.' : '.$email.' : '.$state;
            $stmt->execute();
            }
        catch(PDOException $e)
            {
            echo "Error: " . $e->getMessage();
            }
        $conn = null;

      } else if(isset($error_message)) {
        echo "Error: " . $error_message;
      } else {
        echo "<p>Please fill out the following information below to calculate your results:</p>";
      }
    ?>
    <h2>Calculator</h2>
    <form method="post" action="index.php">
        <table>
            <tr>
                <th><label for="name"> Name </label></th>
                <td><input type="text" id="name" name="name" value="<?php if( isset($name) ){ echo $name; } ?>" /></td>
            </tr>
            <tr>
                <th><label for="email"> Email </label></th>
                <td><input type="text" id="email" name="email" value="<?php if( isset($email) ){ echo $email; } ?>"/></td>
            </tr>
            <tr>
                <th><label for="state"> State </label></th>
                <td>
                  <select id="state" name="state">
                    <option value="AL"<?php if( isset($state) && $state == "AL" ){ echo " selected"; } ?>>Alabama</option>
                    <option value="AK"<?php if( isset($state) && $state == "AK" ){ echo " selected"; } ?>>Alaska</option>
                    <option value="AZ"<?php if( isset($state) && $state == "AZ" ){ echo " selected"; } ?>>Arizona</option>
                    <option value="AR"<?php if( isset($state) && $state == "AR" ){ echo " selected"; } ?>>Arkansas</option>
                    <option value="CA"<?php if( isset($state) && $state == "CA" ){ echo " selected"; } ?>>California</option>
                    <option value="CO"<?php if( isset($state) && $state == "CO" ){ echo " selected"; } ?>>Colorado</option>
                    <option value="CT"<?php if( isset($state) && $state == "CT" ){ echo " selected"; } ?>>Connecticut</option>
                    <option value="DE"<?php if( isset($state) && $state == "DE" ){ echo " selected"; } ?>>Delaware</option>
                    <option value="DC"<?php if( isset($state) && $state == "DC" ){ echo " selected"; } ?>>District Of Columbia</option>
                    <option value="FL"<?php if( isset($state) && $state == "FL" ){ echo " selected"; } ?>>Florida</option>
                    <option value="GA"<?php if( isset($state) && $state == "GA" ){ echo " selected"; } ?>>Georgia</option>
                    <option value="HI"<?php if( isset($state) && $state == "HI" ){ echo " selected"; } ?>>Hawaii</option>
                    <option value="ID"<?php if( isset($state) && $state == "ID" ){ echo " selected"; } ?>>Idaho</option>
                    <option value="IL"<?php if( isset($state) && $state == "IL" ){ echo " selected"; } ?>>Illinois</option>
                    <option value="IN"<?php if( isset($state) && $state == "IN" ){ echo " selected"; } ?>>Indiana</option>
                    <option value="IA"<?php if( isset($state) && $state == "IA" ){ echo " selected"; } ?>>Iowa</option>
                    <option value="KS"<?php if( isset($state) && $state == "KS" ){ echo " selected"; } ?>>Kansas</option>
                    <option value="KY"<?php if( isset($state) && $state == "KY" ){ echo " selected"; } ?>>Kentucky</option>
                    <option value="LA"<?php if( isset($state) && $state == "LA" ){ echo " selected"; } ?>>Louisiana</option>
                    <option value="ME"<?php if( isset($state) && $state == "ME" ){ echo " selected"; } ?>>Maine</option>
                    <option value="MD"<?php if( isset($state) && $state == "MD" ){ echo " selected"; } ?>>Maryland</option>
                    <option value="MA"<?php if( isset($state) && $state == "MA" ){ echo " selected"; } ?>>Massachusetts</option>
                    <option value="MI"<?php if( isset($state) && $state == "MI" ){ echo " selected"; } ?>>Michigan</option>
                    <option value="MN"<?php if( isset($state) && $state == "MN" ){ echo " selected"; } ?>>Minnesota</option>
                    <option value="MS"<?php if( isset($state) && $state == "MS" ){ echo " selected"; } ?>>Mississippi</option>
                    <option value="MO"<?php if( isset($state) && $state == "MO" ){ echo " selected"; } ?>>Missouri</option>
                    <option value="MT"<?php if( isset($state) && $state == "MT" ){ echo " selected"; } ?>>Montana</option>
                    <option value="NE"<?php if( isset($state) && $state == "NE" ){ echo " selected"; } ?>>Nebraska</option>
                    <option value="NV"<?php if( isset($state) && $state == "NV" ){ echo " selected"; } ?>>Nevada</option>
                    <option value="NH"<?php if( isset($state) && $state == "NH" ){ echo " selected"; } ?>>New Hampshire</option>
                    <option value="NJ"<?php if( isset($state) && $state == "NJ" ){ echo " selected"; } ?>>New Jersey</option>
                    <option value="NM"<?php if( isset($state) && $state == "NM" ){ echo " selected"; } ?>>New Mexico</option>
                    <option value="NY"<?php if( isset($state) && $state == "NY" ){ echo " selected"; } ?>>New York</option>
                    <option value="NC"<?php if( isset($state) && $state == "NC" ){ echo " selected"; } ?>>North Carolina</option>
                    <option value="ND"<?php if( isset($state) && $state == "ND" ){ echo " selected"; } ?>>North Dakota</option>
                    <option value="OH"<?php if( isset($state) && $state == "OH" ){ echo " selected"; } ?>>Ohio</option>
                    <option value="OK"<?php if( isset($state) && $state == "OK" ){ echo " selected"; } ?>>Oklahoma</option>
                    <option value="OR"<?php if( isset($state) && $state == "OR" ){ echo " selected"; } ?>>Oregon</option>
                    <option value="PA"<?php if( isset($state) && $state == "PA" ){ echo " selected"; } ?>>Pennsylvania</option>
                    <option value="RI"<?php if( isset($state) && $state == "RI" ){ echo " selected"; } ?>>Rhode Island</option>
                    <option value="SC"<?php if( isset($state) && $state == "SC" ){ echo " selected"; } ?>>South Carolina</option>
                    <option value="SD"<?php if( isset($state) && $state == "SD" ){ echo " selected"; } ?>>South Dakota</option>
                    <option value="TN"<?php if( isset($state) && $state == "TN" ){ echo " selected"; } ?>>Tennessee</option>
                    <option value="TX"<?php if( isset($state) && $state == "TX" ){ echo " selected"; } ?>>Texas</option>
                    <option value="UT"<?php if( isset($state) && $state == "UT" ){ echo " selected"; } ?>>Utah</option>
                    <option value="VT"<?php if( isset($state) && $state == "VT" ){ echo " selected"; } ?>>Vermont</option>
                    <option value="VA"<?php if( isset($state) && $state == "VA" ){ echo " selected"; } ?>>Virginia</option>
                    <option value="WA"<?php if( isset($state) && $state == "WA" ){ echo " selected"; } ?>>Washington</option>
                    <option value="WV"<?php if( isset($state) && $state == "WV" ){ echo " selected"; } ?>>West Virginia</option>
                    <option value="WI"<?php if( isset($state) && $state == "WI" ){ echo " selected"; } ?>>Wisconsin</option>
                    <option value="WY"<?php if( isset($state) && $state == "WY" ){ echo " selected"; } ?>>Wyoming</option>
                  </select>
                </td>
            </tr>
            <tr style="display: none;">
                <th><label for="details"></label></th>
                <td><input type="text" id="details" name="details" />
                  <p>Please leave this field blank.</p>
                </td>
            </tr>
        </table>
        <input type="submit" value="Send" />
    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

只是一点点建议,可以节省你一些时间。你应该用这个:

$stmt = $conn->prepare("INSERT INTO people (name, email, state) 
VALUES (:name, :email, :state)");
$stmt->execute(array(":name"=>$name,":email"=>$email,":state"=>$state);

答案 1 :(得分:0)

如果您的查询出现Integrity Constraint Violation错误,请尝试重新编写您的查询

INSERT INTO people (`name`, `email`, `state`) VALUES (:name, :email, :state);

答案 2 :(得分:0)

我让它工作并插入。我不得不在退出之前将代码移到;命令:

if($mail->send()) {
  //show thank you message
  header('location:index.php?status=thanks');
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "calculations";

  try {
      $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      // prepare sql and bind parameters
      $stmt = $conn->prepare("INSERT INTO people (name, email, state) VALUES (:name, :email, :state)");
      $stmt->bindParam(':name', $name);
      $stmt->bindParam(':email', $email);
      $stmt->bindParam(':state', $state);
      echo $name.' : '.$email.' : '.$state;
      $stmt->execute();
      }
  catch(PDOException $e)
      {
      echo "Error: " . $e->getMessage();
      }
  $conn = null;
  exit;
}

唯一的问题是,如果将来出现插入错误,错误消息将显示在与我想要的不同的位置。我希望它显示在表单上方,因此它不仅仅是一些错误的白页。不过,我会想出来的。我忘了我之前在代码中完成了退出命令。