php - 错误 : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comments' cannot be null

标签 php pdo

我已经尝试检查相关帖子,但它似乎无法帮助我解决手头的问题。我正在尝试为将数据保存在数据库中的约会创建一个页面。然而,这两条错误消息一直出现在我的窗口上:

Notice: Undefined index: comments in C:\xampp\htdocs\db\connect2.php on line 29 Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comments' cannot be null

这是代码 php

<form name="appointments" action="" method="POST">
<label>Name (First/Last)</label> <br>
<input type="text" name="name" id="name" required="required" placeholder="Please Enter Name"> <br>
<label>Email Address</label> <br>
<input type="email" name="email" id="email" required="required" placeholder="youremail@yahoo.com"> <br>
<label>Phone Number</label> <br>
<input type="text" name="contactno" id="contactno" required="required" placeholder="9221234567"> <br>
<label>Nature of Appointment</label> <br>
<select name="service" id="service">
<option value="other">Other</option>
<option value="consultation">Consultation</option>
<option value="Surgery">Surgery</option>
</select> <br>
</div>
<label>Preferred Appointment Date</label> <br>
<input type="date" name="prefDate" id="prefDate"> <br>
<label>Comments</label> <br>
<textarea rows="12" cols="40" name="comments" form="usrform" placeholder="Your comments here..."></textarea> <br>
</div>
<input type="submit" class="btnRegister" name = "schedule" value="Send Your Request">
</form>

这是我的connect2.php

<?php
    //Local Server Information
    $server = "127.0.0.1";
    $username = "root";
    $password = "";
    $db = "myDB";

    $name = "";
    $email = "";
    $contactno = "";
    $service = "";
    $prefDate = "";
    if (isset($_POST['comments']) && !empty($_POST['comments'])) {
        $comments = $_POST['comments'];
    } else {
        $comments = "";
    }
    //Check if connection was successful
    try {
        $con = new PDO("mysql:host=$server;dbname=$db","$username","$password");
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if(isset($_POST['schedule']) )
        {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $contactno = $_POST['contactno'];
            $service = $_POST['service'];
            $prefDate = $_POST['prefDate'];
            $comments = $_POST['comments'];


            $insert = $con->prepare("INSERT INTO appointments(name, email,contactno, service, prefDate, comments) values(:name, :email, :contactno, :service, :prefDate, :comments)");

            $insert->bindParam(':name', $name);
            $insert->bindParam(':email', $email);
            $insert->bindParam(':contactno', $contactno);
            $insert->bindParam(':service', $service);
            $insert->bindParam(':prefDate', $prefDate);
            $insert->bindParam(':comments', $comments);

            $insert->execute();
        }
    } catch(PDOException $e) {
        //die("Oops! Something went wrong with your database.");
        echo "Error: ". $e->getMessage();
    }
?>

这是错误指出问题所在的行。

$comments = $_POST['comments'];

我已经尝试像 $comments="you comment"; 这样努力编码了 它顺利通过,数据出现在数据库中。但是,当我使用上面的代码时,出现错误。请任何人帮助我。我错过了什么?不确定哪里出了问题,因为另一条线路似乎可以正常工作。

谢谢

最佳答案

错误消息告诉自己 - 您试图不在不能为空的列中分配值。解决方案是 - 在您的数据库表中,将默认值设置为 null 到您的 comments 字段。如果你使用 phpmyadmin printscreen

或者你可以运行 mysql 查询(或你使用的其他数据库),像这样

ALTER TABLE <table name> MODIFY COLUMN comments <column type> DEFAULT NULL;

关于php - 错误 : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comments' cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46399519/

相关文章:

php - 如何逻辑地使用 PHP 显示数据库表的所有可能值?

php - PDO postgres 对所有查询不返回任何内容

当 LIMIT 太高时,PHP 的 PDO 查询不执行?

php - 构建 MySQL 表,其中 PLAYLIST 表引用多个 VIDEO id 整数和 USER 表 id

php - 允许带有警报的弹出窗口

php - 搜索并替换 MySQL : nothing was replaced

php - 最后一行在 MYSQL 中返回不正确的值

php - 在特定子字符串之后隔离字符串末尾的子字符串

php - MySQL PDO NOW() 作为指定值 - 这可能吗?

php - 在 CodeIgniter 3 中设置数据库连接超时