2017-08-17 168 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; 
相关问题