I have a mysqli/php code below where it is suppose to insert data into the ‘Session’ Table.
...
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).
//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)
Your input field has
name="totalWeight", but you try to read$_POST['textWeight']It looks like that’s the problem