2017-08-17 226 views
-2

我在哪裏做錯了。我得到這個錯誤:SQLSTATE [23000]:完整性約束違規:1048'duedate'列不能爲空

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'duedate' cannot be null

我的代碼如下,我試圖插入數據到一個MySQL表使用PDO。但它一直在發送一些錯誤,即不能爲空。即使我在表格中輸入duedate。

<?php 
if(empty($_SESSION['studentSession'])){ 
    redirectTo("index.php?tag=login&message=createA"); 
}else{ 
?> 
<!-- <head> --> 
<script type = "text/javascript"> 
var pCount = 2; 
function appendChildtoParent(){ 
    var idValname = "fileToUpload"+pCount; 
    //alert(idValname); 

    var newElem = document.createElement ("div"); 
    newElem.innerHTML = 'Select file to upload:<div class = "form-group">    <input type="file" name='+idValname+' id='+pCount+'></div><div class = "form-group"></div>'; 
    var parentDiv = document.getElementById("parentImageDiv"); 
    parentDiv.appendChild(newElem); 

    var fileUploadAct = document.getElementById("uploadButton1"); 
    fileUploadAct.value = "Upload file(s)"; 

    if(fileUploadAct.disabled = "true"){ 
     fileUploadAct.disabled = false; 
    } 

    pCount = pCount + 1; 
} 

function postHomework(){ 
    var description = tinyMCE.activeEditor.getContent({format : 'html'});  
    description = encodeURIComponent(description); 
    var categories = document.getElementById("categories").value; 
    categories = encodeURIComponent(categories); 
    var question = document.getElementById("questionItself").value; 
    question = encodeURIComponent(question); 
    var memberID = document.getElementById("memberID").value; 
    memberID = encodeURIComponent(memberID); 
    var amount = document.getElementById("amountText").value; 
    amount = encodeURIComponent(amount); 

    //alert(description); 

    if(question == ""){ 
     alert("Enter your question"); 
    } 
    else if(description == ""){ 
     alert("Enter a description"); 
    } 
    else if(categories == ""){ 
     alert("Choose category"); 
    } 
    else if(amount == ""){ 
     alert("Enter amount you are willing to pay"); 
    } 
    else{ 
     var tutorial = document.getElementById("checkboxIns"); 
     if(tutorial.checked){ 
      var tutorialType = "tutorial"; 
     }else{ 
      var tutorialType = "question"; 
     } 

     var emailVal = document.getElementById("emailcheck"); 
     if(emailVal.checked){ 
      var notificatinSend = "1"; 
     } else{ 
      var notificatinSend = "2"; 
     } 
     //alert(notificatinSend); 

     var hr; 
     if (window.XMLHttpRequest){ 
      hr = new XMLHttpRequest(); 
     }else{ 
      hr = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     var url = "posthomeworkAJAX.php"; 

     var vars = "description="+description+"& categories="+categories+"&question="+question+"&memberID="+memberID+"&amount="+amount+"&tutorialType="+tutorialType+"&notificatinSend="+notificatinSend; 

     //var vars = "description="+description; 

     hr.open("POST",url,true); 
     hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     hr.onreadystatechange = function(){ 
     if(hr.readyState == 4 && hr.status == 200){ 
      var return_data = hr.responseText; 
      //document.getElementById("filesUploadBTN").click(); 
      //alert("Saved succeessfully"); 
      //window.location = "my-homework"; 
      document.getElementById("msg").innerHTML = return_data; 
     } 
    } 

    hr.send(vars); 
    document.getElementById("status").innerHTML = "Processing..."; 
    } 
} 

function checkboxIns(){ 
    var chckIns = document.getElementById("checkboxIns"); 
    if(chckIns.checked){ 
     //alert("Checked"); 
     document.getElementById("tutorialCheck").innerHTML = "Check this box only if you are a teacher"; 
     document.getElementById("questionNotification").innerHTML = ""; 
    } 
    else{ 
     document.getElementById("tutorialCheck").innerHTML = ""; 
     document.getElementById("questionNotification").innerHTML = "Do not post private information in a question (e.g. real name, credentials) . Send it directly to the teacher doing your homework later on"; 
     //alert("Unchecked"); 
    } 
} 

function tempSave(attID){ 
    var attVal = document.getElementById(attID).value; 
    if(attVal != ""){ 
     document.getElementById("uploadButton"+attID).value = "Click Submit to upload"; 
     document.getElementById("uploadButton"+attID).disabled = "true"; 
     document.getElementById(attID).visible = "false"; 

     if(pCount > 2){ 
      var i = pCount - 1; 
      document.getElementById("textCount").value = i; 
     } 
    } 
} 
</script> 
<div style="background-color:#eaeef0; width:98%; margin-left:5%; padding: 5% 5%;"> 
<div class = "text-center" id = "msg"></div> 
</br> 
<p style="background-color:#b7d3e1; width:100%; height:50px; padding:5px 5px; text-align:center;font-size:25px;">Create question or Tutorial</p> </br> 
    <label><input type="checkbox" id = "checkboxIns" value="tutorial" onclick = "checkboxIns();" >Tutorial</label> <div id = "tutorialCheck" style = "color: red;"></div> 
    </br> 

    <input type = "hidden" id = "memberID" value = "<?php echo $studentSessionDetails['id'];?>"> 
    <label><h3>Due:</h3></label> </br> 
      <input type = "datetime-local" name= "duedate" > </br> 
    <label><h3>Title:</h3></label> </br> 
    <textarea id = "questionItself" class="form-control" rows="1" id="comment"></textarea> 
    <div id = "questionNotification" style = "color: red;"> 
    Do not post private information in a question (e.g. real name, credentials) . Send it directly to the teacher doing your homework later on 
    </div> 
    </br> 


    <label><h3>Description:</h3></label></br> 
    <textarea name = "homework" id='TypeHere' required = "required"></textarea> 
    </br> 

    <label><input type="checkbox" id = "emailcheck" value="emailnotification" >Yes</label> <div>Would you like to receive notifications by email when writers post answers to your question?</div> 
    </br> 

    <label><h3>Attachments:</h3></label> 
    <div class = "form-inline"> 
    <form method="post" action = "fileUploadhomeWork.php" enctype="multipart/form-data" class = "form-inline" target = "iframe"> 
    <div id = "parentImageDiv"> 
     Select file to upload 
      <div class = "form-group"> 
       <input type="file" name="fileToUpload1" id="1"> 
      </div><div class = "form-group"><input type="submit" value="Upload File(s)" name="submit" class = "btn btn-primary" id = "uploadButton1" onClick = "tempSave('1')"> 
     </div> 
    </div> 
    </div> 
    <input type = "hidden" name = "textCount" id = "textCount" value = "1"> 
    <input type = "hidden" name = "fileUploadFolder" value = "homeworkpost"> 
    <input type = "button" value = "Add Another" onClick = "appendChildtoParent();"/> 
    </br></br> 

    <label>Homework Field of study</label></br> 
    <?php 
    $query = "SELECT categoryname FROM categories"; 
    $statement = $db->_conn->prepare($query); 
    ?> 
    <select id = "categories" class = "form-control" name = "categories"> 
    <option value="">--- Please select ---</option> 
    <?php 
    try{ 
     $statement->execute(); 
     if($statement->rowCount()){ 
      while($r = $statement->fetch(PDO::FETCH_OBJ)){ 
       ?> 
       <option value="<?php echo $r->categoryname;?>"><?php echo $r->categoryname;?></option> 
       <?php 
      } 
     } 
     else{ 
      ?> 
      <option value="">--- No available category ---</option> 
      <?php 
     } 
    } 
    catch(PDOException $e){ 
     echo $e->getMessage(); 
    } 
    ?> 
    </select> 
    </br> 

    <label>I am willing to pay</label> 
    <input type="text" class="form-control" id="amountText"> 
    <input style = "visibility: hidden;" type = "submit" name = "filesUploadBTN" id = "filesUploadBTN" style = "visible: hidden;"/></br> 
    <input class = "btn btn-default" type = "button" id = "BTNPostHomework" name = "BTNPostHomework" value = "Submit" onClick = "postHomework();"/> 
    <input class = "btn btn-default" type = "submit" id = "BTNPreviewHomework" name = "BTNPreviewHomework" value = "Preview"/> 
    </form> 
    </br> 
    <iframe style = "visibility: hidden;" name = "iframe"></iframe> 
    <!-- </form> --> 
    </br> 
    </div> 
    <!-- </div> --> 
    <?php 
} 
    ?> 
+0

您在'var vars'中沒有'duedate'。 – Barmar

回答

1

你打開你的<form>你的交貨期<input>,從而導致傳遞給輸入任何數據表單提交被忽略。

您應該能夠通過將表單的開始標記移動到上方您的第一個表單元素來解決問題。

+0

即使我複製duedate輸入並將其設置在其他輸入下方的任何位置,也會發生相同的錯誤。然後,當我完全刪除它時,數據被插入到數據庫中。只有duedate有問題。 – GuruCoder

+1

他使用AJAX提交表單,而不是正常的表單提交。 – Barmar

+0

'postHomework()'似乎根本不處理'duedate'。據我可以告訴它是從傳遞給AJAX的查詢字符串中丟失... – Bananaapple

0

任何外部輸入表格標記將不會被提交,並且您不會在控制器/ Php端獲得該輸入值。再次

檢查你的代碼,這樣你會得到你有這麼多輸入字段形式 元。該輸入值不會在您的情況下提交。

希望你得到你的解決方案。

+0

但除了duedate之外的其他字段正被插入到數據庫中....您看到下面的表單是用於上傳在提交所有數據之前臨時文件。 – GuruCoder

+0

他使用AJAX提交表單,所以輸入字段的位置並不重要。 – Barmar

1

postHomework()中,您永遠不會提交duedate字段。

你應該給它一個ID,例如:

<input type = "datetime-local" name= "duedate" id="duedate"> 

,然後添加:

var duedate = document.getElementById("duedate").value; 
amount = encodeURIComponent(duedate); 

,並把它添加到POST數據。

var vars = "description="+description+"& categories="+categories+"&question="+question+"&memberID="+memberID+"&amount="+amount+"&tutorialType="+tutorialType+"&notificatinSend="+notificatinSend+"&duedate="+duedate; 
相關問題