PHP错误未定义的索引

时间:2017-11-02 04:04:34

标签: php forms undefined-index

所以我有一个表单和一些PHP代码在数据库中输入信息。但是当我运行php文件时,我得到了这些错误:

  

注意:未定义的索引:第24行的C:\ xampp \ htdocs \ temp \ insert.php

     

注意:未定义的索引:最后在第25行的C:\ xampp \ htdocs \ temp \ insert.php中

     

注意:未定义的索引:第26行的C:\ xampp \ htdocs \ temp \ insert.php中的电子邮件

     

注意:未定义的索引:第27行的C:\ xampp \ htdocs \ temp \ insert.php中的消息

     

致命错误:未捕获PDOException:SQLSTATE [23000]:完整性   约束违规:1048列'第一个'不能为空   C:\ xampp \ htdocs \ temp \ insert.php:29堆栈跟踪:#0   C:\ xampp \ htdocs \ temp \ insert.php(29):PDOStatement-> execute()#1 {main}   在第29行的C:\ xampp \ htdocs \ temp \ insert.php中抛出

这是我的html表单:

<form action="insert.php" method="post">
    <label for="first" >Firstname:</label>
    <input type="text" name="first" id="first" />
    <label for="last" >Surname:</label>
    <input type="text" name="last" id="last" />
    <label for="email" >   Email: </label>
    <input type="text" name="email" id="email" />
    <label for="message">Message:</label>
    <textarea name="message" id="message"> Enter your question here and we will get back
    to you as soon as possible </textarea>
    <input type="Submit">
</form> 

这是我的php:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "test";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully <br />";
    }
    catch(PDOException $e)
    {
        echo "Connection failed: " . $e->getMessage();
    }

    $query=$conn->prepare("INSERT INTO contacts (first, last, email, message) VALUES(?,?,?,?)");
    $query->bindParam(1, $first);
    $query->bindParam(2, $last);
    $query->bindParam(3, $email);
    $query->bindParam(4, $message);

    $first=$_POST['first'];
    $last=$_POST['last'];
    $email=$_POST['email'];
    $message=$_POST['message'];

    $query->execute();

    $conn = null;

    echo 'Hi '.$_POST['first'].' ' .$_POST['last'] .' thanks for your interest.</br>';
    echo 'We will contact you at '. $_POST['email'].' soon.</br>';
?>

1 个答案:

答案 0 :(得分:-1)

编辑:刚认识到您需要仅检查isset :)感谢@phil

试试这段代码:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

try {
 $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 echo "Connected successfully <br />";
 }
catch(PDOException $e)
 {
 echo "Connection failed: " . $e->getMessage();
 }



$query=$conn->prepare("INSERT INTO contacts (first, last, email, message) VALUES(?,?,?,?)");
$query->bindParam(1, $first);
$query->bindParam(2, $last);
$query->bindParam(3, $email);
$query->bindParam(4, $message);

 $first=isset($_POST['first'])?$_POST['first']:"";
$last=isset($_POST['last'])?$_POST['last']:"";
$email=isset($_POST['email'])?$_POST['email']:"";
$message=isset($_POST['message'])?$_POST['message']:"";
$query->execute();

$conn = null;

echo 'Hi '.$_POST['first'].' ' .$_POST['last'] .' thanks for your interest.</br>';
echo 'We will contact you at '. $_POST['email'].' soon.</br>';
?>