由于一列,数据未插入数据库

时间:2012-09-29 19:28:56

标签: php javascript html mysqli

我在下面有一个mysqli / php代码,它假设将数据插入'Session'表。

  ...

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();

            }

问题我得到的是它没有将任何数据插入数据库,原因是它在下面显示了这个错误:

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

发布到“SessionWeight”列中的值来自$_SESSION['textWeight']但它不应显示此错误,因为我已声明如果用户在下面的单选按钮中单击了“否”,则表示该值textbox为0,否则如果用户选择“Yes”,则用户必须输入他们自己的图(不允许使用空白文本框)。

//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;

        }

    }

数据库中的SessionWeight列是一个非关键字段,数据类型为int(3)

1 个答案:

答案 0 :(得分:0)

您的输入字段为name="totalWeight",但您尝试阅读$_POST['textWeight']

看起来这就是问题