0

我在下面有一个 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();

            }

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

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

发布到“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;

        }

    }

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

4

1 回答 1

0

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

好像是这个问题

于 2012-09-29T20:39:51.443 回答