2017-08-17 1 views
-2

Wo könnte ich es falsch machen. Ich erhalte diese Fehlermeldung:SQLSTATE [23000]: Verletzung Integritätsbedingung: 1048 Spalte 'duedate' kann nicht null sein

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

Mein Code ist wie folgt und ich versuche, Daten in eine MySQL-Tabelle einfügen mit PDO. Aber es sendet immer wieder einen Fehler, dass duedate nicht leer sein kann. Auch wenn ich Duedate in das Formular eingebe.

<?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

Sie haben 'duedate' nicht in' var vars'. – Barmar

Antwort

1

Sie öffnen Ihre <form>nach Ihre duedate <input>, sodass sämtliche Daten verursacht an den Eingang weitergegeben Form Vorlage ignoriert werden.

Sie sollten in der Lage sein, das Problem zu beheben, indem Sie das öffnende Tag des Formulars verschieben, um über Ihr erstes Formularelement zu setzen.

+0

Auch wenn ich den Duedate-Eingang kopiere und ihn irgendwo unter andere Eingänge setze, tritt derselbe Fehler auf. Wenn ich es dann vollständig entferne, werden Daten in die db eingefügt. Nur duedate hat Probleme. – GuruCoder

+1

Er reicht das Formular mit AJAX ein, nicht normale Formularübermittlung. – Barmar

+0

'postHomework()' scheint 'duedate' überhaupt nicht zu behandeln. Soviel ich sagen kann, fehlt es an der Query-Zeichenfolge, die an AJAX übergeben wird ... – Bananaapple

0

Jeder Eingang außerhalb Formular Tag wird nicht eingereicht und Sie erhalten diesen Eingangswert nicht in Ihrem Controller/PHP-Seite.

überprüfen wieder Ihren Code so erhalten Sie, dass Sie außerhalb Form Element so viele Eingabefeld haben. Diese Eingabewerte werden in Ihrem Fall nicht übermittelt.

Hoffen Sie, dass Sie Ihre Lösung bekommen.

+0

Aber die anderen Felder außer dem Duedaten werden in die Datenbank eingefügt .... Das Formular, das Sie unten sehen, ist eins für das Hochladen der Datei vorübergehend, bevor ich alle Daten einreiche. – GuruCoder

+0

Er sendet das Formular mit AJAX, es ist also egal, wo die Eingabefelder sind. – Barmar

1

In postHomework() senden Sie nie das Feld duedate.

Sie sollten es eine ID geben, z.

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

und fügen Sie dann:

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

und fügen Sie den POST Daten.

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