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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀