我有一個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']
但它不應該顯示此錯誤,因爲我已經聲明如果用戶點擊了下面的單選按鈕中的」否「,那麼文本框中的值是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)
什麼HAP如果javascript被禁用,如何使用筆?您需要客戶端驗證。 – BillThor