1

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.

  • 3
    Why are you storing the connection in the session to begin with? – CBroe Jan 25 '21 at 13:06
  • 1
    Oh, maybe you are assuming that you can store a mysqli connection in the session object and reuse it in any script. Is that what you are trying to do?? I am afraid you **cannot do that** – RiggsFolly Jan 25 '21 at 13:14
  • I figured it was better since that way I only had to define it once in a seperate file and I can us it wherever I want. It worked everywhere else where I used it. Anyway, I defined $conn in the file itself and that seemed to have fixed it. – Dirk Freijters Jan 25 '21 at 13:26
  • The session is basically just a file. Once script1 that makes the connection terminates the connection is automatically closed by PHP as the script goes out of scope. The connection is NOT reopened just because you reconnect to the session, at that point its just a bit of useless data – RiggsFolly Jan 25 '21 at 13:33
  • Ohh, that makes a lot of sense! In all files where it did work I included the file in which the connection was made, which prevented it from going out of scope? Thank you anyway! :) – Dirk Freijters Jan 25 '21 at 13:47

0 Answers0