SQLSTATE [23000]:完整性约束违规:1048列'duedate'不能为空

时间:2017-08-17 08:40:16

标签: php arrays mysqli pdo

我在哪里做错了。我收到了这个错误:

  

SQLSTATE [23000]:完整性约束违规:1048列'duedate'不能为空

我的代码如下,我试图使用PDO将数据插入到mysql表中。但它不断发送一些错误,duedate不能为空。即使我在表格中输入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
 }
  ?>

3 个答案:

答案 0 :(得分:1)

您在您的专属<form>之后打开了<input> ,从而导致在表单提交时忽略传递给输入的任何数据。

您应该可以通过将表单的开始标记移动到上面第一个表单元素来解决问题。

答案 1 :(得分: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;

答案 2 :(得分:0)

表单标记之外的任何输入都不会被提交,您将无法在控制器/ Php端获得该输入值。

再次检查您的代码,这样您就可以在表单之外获得输入字段 元件。在您的情况下,不会提交输入值。

希望你能得到解决方案。