gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:15:20 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com