错误:SQLSTATE [23000]:完整性约束违规:1048'comments'列不能为空
我已经尝试检查相关帖子,但似乎无法帮助我解决手头上的问题。我正在尝试创建一个页面,用于将数据保存在数据库中的约会。然而,这两个错误消息连续出现我的窗口:错误: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="[email protected]"> <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 =“你评论”; 它经历了没有错误,数据出现在数据库中。但是,当我使用上面的代码时,出现错误。任何人都可以帮助我。我错过了什么?不知道自从另一条线似乎工作以来出了什么问题。
感谢
所以试图修复它运用你的建议。并且它似乎是我在php下输入的这部分代码中的错误的责任人。
<textarea rows="12" cols="40" name="comments" form="usrform" placeholder="Your comments here..."></textarea>
是
form="usrform"
删除它只是使代码工作。似乎表单停止在prefDate中,因为我在textarea标签上偶然分配了不同的表单名称。
感谢谁给提醒,让我看到了什么错误
你可以试试这个:
$comments = !empty($_POST['comments']) ? $_POST['comments'] : 'Default Comment';
错误消息告诉自己 - 你想不分配列值不能为空。解决方案是 - 在您的数据库表中,将缺省值设置为空到您的comments
字段。如果你用phpmyadmin
,或者你可以运行mysql
查询(或其他数据库,您可以使用),这样的事情
ALTER TABLE <table name> MODIFY COLUMN comments <column type> DEFAULT NULL;
中输入的内容,我试过这样做,说错误消失了。但是,除了显示为空的注释以外,数据现在会出现在数据库中。 T^T – UmaruHime
,它应该如何工作。如果你没有默认值(在你的情况下为空),你不指定任何东西 - 提交将有空值。如果你想要空字符串'''''你可以运行'ALTER TABLE
您好我试着这样做的两个错误:SQLSTATE [23000]:完整性约束违规:1048列“意见”不能为空 错误消失而且它没有任何错误。然而,评论只插入“默认评论”,而不是我在 – UmaruHime