douqin231881 2019-05-25 16:54
浏览 209
已采纳

在每个foreach中使用多个foreach进行插入只能获得最终值

I am currently working on a evaluation system. The questions are dynamically created/

So I already tried insert values from different foreach of input fields I use input type text instead of radio button. my code is just inserting the last value get from each foreach loop.

 <?php 
    session_start();
    include('connectDB.php');
    $userId= $_SESSION['id'];

    mysqli_query($conn,"UPDATE student SET evaluationId = 1 WHERE studentId='$userId'");

    foreach($_POST['nPersonnelId'] as $personnelId ){
        for ($i=0; $i<count($_POST['rCourteous']); $i++){
            $rCourteous = $_POST['rCourteous'][$i];
        }
        for ($i=0; $i<count($_POST['rPrompt']); $i++){
            $rPrompt = $_POST['rPrompt'][$i];
        }
        for ($i=0; $i<count($_POST['rCompetent']); $i++){
            $rCompetent = $_POST['rCompetent'][$i];
        }
        for ($i=0; $i<count($_POST['rAccom']); $i++){
            $rAccom = $_POST['rAccom'][$i];
        }
        mysqli_query($conn,"INSERT INTO qpanswer
                (studentId,personnelId,courteous,prompt,competent,accommodating) 
                VALUES ('$userId','$personnelId','$rCourteous','$rPrompt','$rCompetent','$rAccom')");
    }
    header('location:studentDashboard.php');
?>
  • 写回答

1条回答 默认 最新

  • dongmi3203 2019-05-25 17:00
    关注

    You don't need all of the inner loops, just get the index from the outer loop and use that for all of the inner arrays. So here get $i from the foreach key...

    foreach($_POST['nPersonnelId'] as $i => $personnelId ){
        $rCourteous = $_POST['rCourteous'][$i];
    

    Same for each of the other fields.

    Also as pointed out - use prepared statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题