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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程