错误:SQLSTATE [23000]:违反完整性约束:1048列"comments"不能为空 [英] Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comments' cannot be null

查看:150
本文介绍了错误:SQLSTATE [23000]:违反完整性约束:1048列"comments"不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试检查相关的帖子,但似乎并不能帮助我解决当前的问题.我正在尝试为约会创建一个页面,在该页面中将数据保存在数据库中.但是,这两个错误消息一直出现在我的窗口中:

I have already tried checking for related post but it doesn't seem to help me fix my problem at hand. I am trying to create an page for appointments where data are saved in the database. however, these two error message kept appearing on my window:

注意:未定义索引:C:\ xampp \ htdocs \ db \ connect2.php中的注释 第29行错误:SQLSTATE [23000]:违反完整性约束: 1048列"comments"不能为空

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();
    }
?>

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

This is the line where the error says the problem was.

$ comments = $ _POST ['comments'];

$comments = $_POST['comments'];

我已经尝试过像$ comments ="you comment"这样的硬编码; 它没有任何错误,并且数据显示在数据库中.但是,当我使用上面的代码时,会出现错误.任何人都可以帮助我.我错过了什么?不确定发生了什么错误,因为另一行似乎可行.

I already tried hard coding it like $comments="you comment"; It went through with no errors and the data appears on the database. However, when I use the code above, error appears. Anyone help me please. Did i miss something? NOt sure what went wrong since the other line seems to work.

谢谢

推荐答案

因此请尝试根据您的建议进行修复.似乎是我在php中输入的这段代码错误的原因.

So tried to fix it applying your suggestions. and it seems the one responsible for the error in this portion of the code I entered under php.

<textarea rows="12" cols="40" name="comments" form="usrform" placeholder="Your comments here..."></textarea>

form ="usrform"

form="usrform"

删除它只会使代码正常工作.似乎该表单停止在prefDate中,因为我在textarea标记上偶然分配了另一个表单名称.

removing it simply made the code to work. it seems that the form stops in the prefDate because i assigned a different form name by accident on the textarea tag.

感谢提供建议的两个人,这些建议让我知道出了什么问题

thanks the two who gave advise that made me see what went wrong

这篇关于错误:SQLSTATE [23000]:违反完整性约束:1048列"comments"不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆