I have a mysqli/php code below where it is suppose to insert data into the ‘Session’ Table. Now it used to insert data with no problems previously and I have not changed the code below:
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("sssssssss", $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();
}
}
Like I said this code worked before and I have not changed anything in the code since. But I cannot remember if its because I change the table in the database (phpmyadmin) that it is not inserting data into the database.
Can anyone check the Table creation before and state if you see any problems? Could the problem be with other tables which contains SessionId as their foriegn key?
Below is the Table Creation using SHOW CREATE TABLE Session:
CREATE TABLE `Session` (
`SessionId` varchar(10) NOT NULL,
`SessionTime` time NOT NULL,
`SessionDate` date NOT NULL,
`SessionWeight` int(3) NOT NULL,
`SessionDuration` time NOT NULL,
`TotalMarks` int(5) NOT NULL,
`FileId` varchar(10) DEFAULT NULL,
`ModuleId` varchar(10) NOT NULL,
`TeacherId` int(4) NOT NULL,
`Room` varchar(10) NOT NULL,
PRIMARY KEY (`SessionId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Based on the information you are giving us, we have to make the following assumptions:
With those assumptions the variable $teacherId is not being set, so it’s value is null. In your table create statement the TeacherID column does not accept a null value so the insert is failing.