//connected to db
if (isset($_POST['teacherusername'])) {
$_SESSION['teacherusername'] = $_POST['teacherusername'];
}
$sql = "SELECT TeacherId FROM Teacher WHERE (TeacherUsername = ?)";
$stmt=$mysqli->prepare($sql);
$stmt->bind_param("s",$_SESSION['teacherusername']);
$stmt->execute();
$record = $stmt->fetch();
$teacherid = $record['TeacherId'];
$stmt->close();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$time = str_replace(array(' Hrs ', ' Mins ', ' Secs'), array(':', ':', ''), $_SESSION['durationChosen']);
$insertsql = "INSERT INTO Session (SessionId, SessionTime, SessionDate, SessionWeight, SessionDuration, TotalMarks, ModuleId, TeacherId, Room) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$insert = $mysqli->prepare($insertsql);
for ($i = 1, $n = $_SESSION['sessionNum']; $i <= $n; ++$i) {
$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) { echo "Error in insert: $insert->error<br>\r\n"; }
$insert->close();
}
}
UPDATE:
The previous errors have been fixed, but I am now getting 4 warnings which are displayed below:
Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Couldn’t fetch mysqli_stmt in /web/stud/…/Mobile_app/insertsession.php on line 177
Warning: mysqli_stmt::execute() [mysqli-stmt.execute]: Couldn’t fetch mysqli_stmt in /web/stud/…/Mobile_app/insertsession.php on line 179
Warning: main() [function.main]: Couldn’t fetch mysqli_stmt in /web/stud/…/Mobile_app/insertsession.php on line 181
Warning: mysqli_stmt::close() [mysqli-stmt.close]: Couldn’t fetch mysqli_stmt in /web/stud/…/Mobile_app/insertsession.php on line 185
As that mysqli is now inserting data into the database, do I deal with these warnings or shall I leave them alone?
PROBLEM IS SOLVED, FOR FUTURE VIEWERS PLEASE LOOK AT DAVE RANDOM’S ANSWER
Simply add this line at the end of your loop and it should work:
This will work if you don’t still have an unclosed statement from code that is executed before the code you show. You must close the previous statement before another can be executed.
See
mysqli_stmt::close()for more information.EDIT
Try this code: