For the life of me I can't figure out why my code gives the error:
Fatal error: Uncaught Error: mysqli object is already closed in C:\xampp\htdocs.apps\DeSplinterRekenen\Code\Teacher\manageTeachersSave.php:3 Stack trace: #0 C:\xampp\htdocs.apps\DeSplinterRekenen\Code\Teacher\manageTeachersSave.php(3): mysqli_stmt_init(Object(mysqli)) #1 {main} thrown in C:\xampp\htdocs.apps\DeSplinterRekenen\Code\Teacher\manageTeachersSave.php on line 3
manageTeachersSave.php:
<?php
session_start();
for ($j=0; $j<$_SESSION['i']; $j++){
$stmt = mysqli_stmt_init($_SESSION['conn']);
mysqli_stmt_prepare($stmt, "UPDATE accounts SET perms = ? WHERE id = ?");
mysqli_stmt_bind_param($stmt, "is", $_GET['perms' . $j], $_SESSION['teachRow']['id']);
mysqli_stmt_execute($stmt);
echo "Set the perms for user with id " . $_SESSION['teachRow']['id'] . " to " . $_GET['perms' . $j];
}
//header("Location: manageTeachers.php");
I'm trying to update the column 'perms' to the correct value for every user based off of this form:
<form action="manageTeachersSave.php">
<table class="teachers">
<tr>
<th>G</th>
<th>L</th>
<th>AB</th>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Email</th>
</tr>
<?php
$teacher = 1;
$teacher2 = 2;
$stmt = mysqli_stmt_init($_SESSION['conn']);
mysqli_stmt_prepare($stmt, "SELECT * FROM accounts WHERE (teacher=? OR teacher=?) AND id!=?");
mysqli_stmt_bind_param($stmt, "iii", $teacher, $teacher2, $_SESSION['loggedID']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$_SESSION['teachRow'] = mysqli_fetch_assoc($result);
$i = 0;
do {
echo "<tr>";
?>
<td><label><input type="radio" name="perms<?php echo $i;?>" value="G" <?php if($_SESSION['teachRow']['perms'] == 0){echo "checked";}?>></label></td>
<td><label><input type="radio" name="perms<?php echo $i;?>" value="L" <?php if($_SESSION['teachRow']['perms'] == 1){echo "checked";}?>></label></td>
<td><label><input type="radio" name="perms<?php echo $i;?>" value="AB" <?php if($_SESSION['teachRow']['perms'] == 2){echo "checked";}?>></label></td>
<?php
echo "<td>" . $_SESSION['teachRow']['firstName'] . "</td>";
echo "<td>" . $_SESSION['teachRow']['lastName'] . "</td>";
echo "<td>" . $_SESSION['teachRow']['email'] . "</td>";
echo "</tr>";
$i++;
} while($_SESSION['teachRow'] = mysqli_fetch_array($result));
?>
</table>
<label><input id="teacherSaveButton" type="submit" value="Veranderingen opslaan"></label>
</form>
I tried to move the mysqli stuff out of the for-loop but that resulted in the same error.