douhui3760 2016-05-26 09:57
浏览 27
已采纳

MySQL多行更新失败

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.

  • 写回答

2条回答 默认 最新

  • dongshao6591 2016-05-26 10:08
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)