简体   繁体   English

数据由于一列而未插入数据库内部

[英]data not being inserted inside the database because of one column

I have a mysqli/php code below where it is suppose to insert data into the 'Session' Table. 我下面有一个mysqli / php代码,它是在“会话”表中插入数据的地方。

  ...

if (isset($_POST['textWeight'])) {

$_SESSION['textWeight'] = $_POST['textWeight'];

}

...

        if($_SERVER['REQUEST_METHOD'] == 'POST')

        {

            $time = str_replace(array(' Hrs ', ' Mins ', ' Secs'), array(':', ':', ''), $_SESSION['durationChosen']);

            for ($i = 1, $n = $_SESSION['sessionNum']; $i <= $n; ++$i) {

                    $insertsql = "
              INSERT INTO Session
                (SessionId, SessionTime, SessionDate, SessionWeight, SessionDuration, TotalMarks, ModuleId, TeacherId, Room)
              VALUES
                (?, ?, ?, ?, ?, ?, ?, ?, ?)
            ";
            if (!$insert = $mysqli->prepare($insertsql)) {
              // Handle errors with prepare operation here
            }

                $sessid = $_SESSION['id'] . ($n == 1 ? '' : $i);
                $sessdate = date("Y-m-d", strtotime($_SESSION['dateChosen']));

                $insert->bind_param("sssisisis", $sessid, $_SESSION['timeChosen'], $sessdate,
                             $_SESSION['textWeight'], $time, $_SESSION['textMarks'],
                             $_SESSION['module'], $teacherid, $_SESSION['rooms']);

                $insert->execute();

                if ($insert->errno) {
                  // Handle query error here
                }

                $insert->close();

            }

Problem I am getting is that it is not inserting any data into the database and the reason for this is because it is displaying this error below: 我得到的问题是它没有在数据库中插入任何数据,其原因是因为它在下面显示此错误:

Warning: mysqli_stmt::execute(): (23000/1048): Column 'SessionWeight' cannot be null in /web/stud/..../.... on line 182

The values posted into the "SessionWeight" column comes from $_SESSION['textWeight'] But it should not display this error because I have stated that if user has clicked on "No" in the radio button below, then the value in the textbox is 0, else if the user selects "Yes" then the user must enter in their own figure (blank textbox not allowed). 张贴到“ SessionWeight”列中的值来自$_SESSION['textWeight']但不应显示此错误,因为我已声明如果用户单击下面的单选按钮中的“否”,则该文本框中的值为0,否则,如果用户选择“是”,则用户必须输入自己的图形(不允许使用空白文本框)。

//HTML
        <th>Provide a Total Weight Assessment is worth to Module?</th>
        <td><input type="radio" name="weightChoice" value="Yes" onClick="getWeight()" class="radioBtn"/> Yes</td>
            <td><input type="radio" name="weightChoice" value="No" onClick="getWeight()" class="radioBtn"/> No</td>
            </tr>
            </table>
            <div id="radioAlert"></div>
            <p></p>
        <table id="tblWeight">
        <tr>
        <th>Weight:</th>
            <td><input type="text" id="txtWeight" name="totalWeight" onkeypress="return isNumberKey(event)" maxlength="5" />%</td>
            </tr>
            </table>

.... ....

//Javascript

        function getWeight() {
            var weightChoice = document.getElementsByName("weightChoice");               
            var textWeight = document.getElementById("txtWeight");
            var tblWeight = document.getElementById("tblWeight");

            if(weightChoice[0].checked == true){
                tblWeight.style.display = "block";
                textWeight.value = "";
            }else{
                tblWeight.style.display = "none";
                textWeight.value = 0;

        }

    }

The SessionWeight column in the database is a non-key field which data type is int(3) 数据库中的SessionWeight列是一个非关键字段,其数据类型为int(3)

Your input field has name="totalWeight" , but you try to read $_POST['textWeight'] 您的输入字段具有name="totalWeight" ,但是您尝试读取$_POST['textWeight']

It looks like that's the problem 看起来就是问题所在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM