Mena Mena - 6 months ago 16
MySQL Question

MySQL Multiple row update failure

Am attempting to update multiple records in a table all at once when a user clicks on the submit button of the form. My code only updates the first row and does nothing to the rest, no error. Below is the code am working with on post

<?php
if (isset($_POST['submit'])) {

$subject_id = $_POST["subject_id"];
$student_id = $_POST["student_id"];
$test1 = $_POST["test1"];
$test2 = $_POST["test2"];
$test3 = $_POST["test3"];

for($i=0; $i < count($student_id); $i++) {
$studentid = mysqli_real_escape_string($connection, $student_id[$i]);
$subjectid = mysqli_real_escape_string($connection, $subject_id);
$test_1 = mysqli_real_escape_string($connection, $test1[$i]);
$test_2 = mysqli_real_escape_string($connection, $test2[$i]);
$test_3 = mysqli_real_escape_string($connection, $test3[$i]);

$query = "UPDATE records SET test1='{$test_1}', test2='{$test_2}', test3='{$test_3}' WHERE student_id={$studentid} AND subject_id={$subjectid}";
$result = mysqli_query($connection, $query);

if ($result) {
redirect_to("dashboard.php");
} else {
echo 'MySQL Error: ' . mysqli_error($connection);
exit;
}
}
}
?>


What could be responsible for only the first row being updated and the others ignored? Questions similar to multiple row update haven't helped much as they seem different from what am trying to do here.

Answer

In loop you made redirect_to("dashboard.php"); so it probably exit the loop ;)