我已經嘗試檢查相關帖子,但似乎無法幫助我解決手頭上的問題。我正在嘗試創建一個頁面,用於將數據保存在數據庫中的約會。然而,這兩個錯誤消息連續出現我的窗口:錯誤: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 =「你評論」; 它經歷了沒有錯誤,數據出現在數據庫中。但是,當我使用上面的代碼時,出現錯誤。任何人都可以幫助我。我錯過了什麼?不知道自從另一條線似乎工作以來出了什麼問題。
感謝
您好我試着這樣做的兩個錯誤:SQLSTATE [23000]:完整性約束違規:1048列「意見」不能爲空 錯誤消失而且它沒有任何錯誤。然而,評論只插入「默認評論」,而不是我在 – UmaruHime