Below I have a modules drop down menu where it gets a list of modules from the db an obviously displays them in a drop down menu. It also displays the selected module number and module name in the $output variable and in a hidden input ($hiddenoutput) it displays the module id of the selected module (This was successfully texted when text input was text before changing to hidden)
$active = 1;
$sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->bind_param("i",$active);
$sqlstmt->execute();
$sqlstmt->bind_result($dbModuleId, $dbModuleNo, $dbModuleName);
$modules = array(); // easier if you don't use generic names for data
$output = "";
$hiddenoutput = "";
$moduleHTML = "";
$moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while($sqlstmt->fetch())
{
$moduleno = $dbModuleNo;
$module = $dbModuleId;
$modulename = $dbModuleName;
$moduleHTML .= "<option value='".$module."'>" . $moduleno . " - " . $modulename . "</option>".PHP_EOL;
if (isset($_POST['module']) && ($_POST['module'] == $module)) {
$output .= "<p><strong>Selected Module:</strong> " . $moduleno . " - " . $modulename . "</p>";
$hiddenoutput .= "<p><input type='hidden' id='hiddenoutput' name='hiddenmodule' value='". $module ."'></p>";
}
}
$moduleHTML .= '</select>';
$submittedModuleId = (isset($_POST['module'])) ? $_POST['module'] : '';
$output = (isset($output)) ? $output : '';
$hiddenoutput = (isset($hiddenoutput)) ? $hiddenoutput : '';
The details are displayed in the form below:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Module: <?php echo $moduleHTML; ?><input id="moduleSubmit" type="submit" value="Submit" name="submit" /></th>
</tr>
<tr>
<td><span id="errormsg"><?php echo $errormsg; ?></span><?php echo $hiddenoutput . $output; ?></td>
</tr>
</table>
</form>
But what I really do not understand is that in my INSERT sql below, it is able to retrieve and insert all values except for one and that is the value of the Module Id displayed in the hidden input. It keeps stating:
Warning: mysqli_stmt::execute(): (23000/1048): Column 'ModuleId' cannot be null in ... on line 172
What my question is that why is can not it retrieve and insert the value in the database for the module id? Below is the code:
session_start();
if (isset($_POST['hiddenmodule'])) {
$_SESSION['hiddenmodule'] = $_POST['hiddenmodule'];
}
if (isset($_POST['id'])) {
$_SESSION['id'] = $_POST['id'];
}
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = $_POST['sessionNum'];
}
if (!isset($_SESSION['sessionCount'])) {
$_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
++$_SESSION['sessionCount'];
}
$sessionMinus = $_SESSION['sessionCount'];
if (isset($_POST['durationChosen'])) {
$_SESSION['durationChosen'] = $_POST['durationChosen'];
}
if (isset($_POST['dateChosen'])) {
$_SESSION['dateChosen'] = $_POST['dateChosen'];
}
if (isset($_POST['timeChosen'])) {
$_SESSION['timeChosen'] = $_POST['timeChosen'];
}
if (isset($_POST['textQuestion'])) {
$_SESSION['textQuestion'] = $_POST['textQuestion'];
}
if (isset($_POST['textMarks'])) {
$_SESSION['textMarks'] = $_POST['textMarks'];
}
if (isset($_POST['totalWeight'])) {
$_SESSION['totalWeight'] = $_POST['totalWeight'];
}
if (isset($_POST['buildings'])) {
$_SESSION['buildings'] = $_POST['buildings'];
}
if (isset($_POST['rooms'])) {
$_SESSION['rooms'] = $_POST['rooms'];
}
if (isset($_POST['teacherforename'])) {
$_SESSION['teacherforename'] = $_POST['teacherforename'];
}
if (isset($_POST['teachersurname'])) {
$_SESSION['teachersurname'] = $_POST['teachersurname'];
}
if (isset($_POST['teacherusername'])) {
$_SESSION['teacherusername'] = $_POST['teacherusername'];
}
var_dump($_POST);
var_dump($_SESSION);
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$sql = "
SELECT TeacherId
FROM Teacher
WHERE TeacherUsername = ?
";
if (!$stmt = $mysqli->prepare($sql)) {
// Handle errors with prepare operation here
}
// Bind parameter for statement
$stmt->bind_param("s", $_SESSION['teacherusername']);
// Execute the statement
$stmt->execute();
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$stmt->bind_result($teacherid);
// This populates $teacherid
$stmt->fetch();
// Close the statment
$stmt->close();
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
(SessionName, SessionTime, SessionDate, SessionWeight, SessionDuration, TotalMarks, ModuleId, TeacherId, Room)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$sessname = $_SESSION['id'] . ($n == 1 ? '' : $i);
$sessdate = date("Y-m-d", strtotime($_SESSION['dateChosen']));
$insert->bind_param("sssisiiis", $sessname, $_SESSION['timeChosen'], $sessdate,
$_SESSION['totalWeight'], $time, $_SESSION['textMarks'],
$_SESSION['hiddenmodule'], $teacherid, $_SESSION['rooms']);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}
}
Below is what the var_dump($_SESSION) outputs:
array(17) {
["teacherid"]=> int(49)
["teacherusername"]=> string(8) "u0867587"
["teacherforename"]=> string(5) "Bruce"
["teachersurname"]=> string(5) "Finch"
["id"]=> string(5) "XNTLR"
["initial_count"]=> string(1) "1"
["sessionNum"]=> string(1) "1"
["sessionCount"]=> int(1)
["durationChosen"]=> string(22) "02 Hrs 00 Mins 00 Secs"
["dateChosen"]=> string(10) "15-12-2012"
["timeChosen"]=> string(5) "10:00"
["textQuestion"]=> string(1) "1"
["textMarks"]=> string(1) "5"
["totalWeight"]=> string(1) "0"
["buildings"]=> string(14) "Canalside East"
["rooms"]=> string(7) "CE01/04"
["hiddenmodule"]=> NULL }
Because hiddenmodule is is not posted. See your var dump. Check the form with firebug to see if this input field is add