2017-09-25 192 views
3

我已經嘗試檢查相關帖子,但似乎無法幫助我解決手頭上的問題。我正在嘗試創建一個頁面,用於將數據保存在數據庫中的約會。然而,這兩個錯誤消息連續出現我的窗口:錯誤: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 =「你評論」; 它經歷了沒有錯誤,數據出現在數據庫中。但是,當我使用上面的代碼時,出現錯誤。任何人都可以幫助我。我錯過了什麼?不知道自從另一條線似乎工作以來出了什麼問題。

感謝

回答

0

所以試圖修復它運用你的建議。並且它似乎是我在php下輸入的這部分代碼中的錯誤的責任人。

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

form="usrform"

刪除它只是使代碼工作。似乎表單停止在prefDate中,因爲我在textarea標籤上偶然分配了不同的表單名稱。

感謝誰給提醒,讓我看到了什麼錯誤

1

你可以試試這個:

$comments = !empty($_POST['comments']) ? $_POST['comments'] : 'Default Comment'; 
+0

您好我試着這樣做的兩個錯誤:SQLSTATE [23000]:完整性約束違規:1048列「意見」不能爲空 錯誤消失而且它沒有任何錯誤。然而,評論只插入「默認評論」,而不是我在 – UmaruHime

1

錯誤消息告訴自己 - 你想不分配列值不能爲空。解決方案是 - 在您的數據庫表中,將缺省值設置爲空到您的comments字段。如果你用phpmyadmin printscreen

,或者你可以運行mysql查詢(或其他數據庫,您可以使用),這樣的事情

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

中輸入的內容,我試過這樣做,說錯誤消失了。但是,除了顯示爲空的註釋以外,數據現在會出現在數據庫中。 T^T – UmaruHime

+0

,它應該如何工作。如果你沒有默認值(在你的情況下爲空),你不指定任何東西 - 提交將有空值。如果你想要空字符串'''''你可以運行'ALTER TABLE MODIFY COLUMN註釋 NOT NULL DEFAULT'';' – 2017-09-25 07:56:25

相關問題