php - 当值不为空时,发生完整性约束冲突

标签 php mysql pdo

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

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>

最佳答案

只是一个可以节省您时间的小建议。你应该使用这个:

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

关于php - 当值不为空时,发生完整性约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37470230/

相关文章:

php - 如何在 PHP 中使用 CSSModules(非 SPA)

PHP SSH2 PECL 扩展问题

php - 在 PHP 代码库中查找所有字符串

mysql - 如何将 varchar 转换为 datetime,不起作用

php - 如何使用 php 从数据库中获取类别列表?

php - 从数据库获取信息时遇到问题

php - curl 到 node.js 的服务器。但不返回任何东西。如何让 server.js 返回 200 json?

php - PDO 无法访问数据库连接实例

php - undefined index 和 undefined variable ,但两者都已定义! (我认为....)

php - MySQL 错误 1064 使用 LIMIT 子句