0

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.

1
  • What about this line. $subjectid = mysqli_real_escape_string($connection, $subject_id); You have not passed the $i in to $subject_id? Commented May 26, 2016 at 10:00

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Closing the loop before redirecting solved it. Thanks mate
0

Redirect condition placed wrongly. Find the below code. 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) { 
        break;
    } 
}
if(!$result){
  echo 'MySQL Error: ' . mysqli_error($connection);
  exit;
}
else {
   redirect_to("dashboard.php");
}} ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.