doubaisui2526 2019-05-15 14:29
浏览 382
已采纳

使用foreach更新多个记录

I have a problem with a part of the code in which, through a foreach I go through an array and those that meet certain conditions are updated by an UPDATE value. The problem is that I try to pass the values ​​with a hidden input and only update the last value. How can I modify the code so I can do it with arrays?

try {
    $add = "UPDATE calculo SET puntaje_fecha=:puntaje_fecha WHERE id_calculo=:id_calculo";
    $d = $conn->prepare($add);
    $d->bindParam(':puntaje_fecha', $_POST['puntaje_fecha']);
    $d->bindParam(':id_calculo', $_POST['id_calculo']);
    $d->execute();

} catch (PDOException $e) {
    die("Error: " . $e->getMessage() . "<br>on line: " . $e->getLine() . "<br>on file: " . $e->getFile());
}

<form action="actualizar.php" method="post">
      <?php foreach ($ca as $c){?>
        <?php foreach ($pr as $p) {}?>
      <?php if (($p['id_fecha'])==($c['nFecha'])){
        //actions to be taken if the conditions are met

                  }?>

                  <input type="hidden" name="puntaje_fecha" value="<?php echo $s; ?>">
                  <input type="hidden" name="id_calculo" value="<?php echo $c['id_calculo']; ?>">

      <?php }} ?>

      <input type="submit" value="Actualiza puntos">
    </form>

What I need is that in my "calculo" table all the values ​​of "puntaje_fecha" are updated as long as they meet that condition. Many people have recommended me to change the name of the input by arrays, but I do not know how to do it. Thank you very much to anyone who can help me.

  • 写回答

2条回答 默认 最新

  • doudianhuo1129 2019-05-15 15:03
    关注

    You are creating only two inputs with the names of "puntaje_fecha" and "id_calculo".
    So value from last iteration is posted, in order to post all values change the hidden inputs to array.

              <input type="hidden" name="puntaje_fecha[]" value="<?php echo $s; ?>">
              <input type="hidden" name="id_calculo[]" value="<?php echo $c['id_calculo']; ?>">
    



    Make sure your inputs look like below by inspect element, otherwise check your data in $ca and $pr

              <input type="hidden" name="puntaje_fecha[]" value="value_1">
              <input type="hidden" name="id_calculo[]" value="value_1">
              <input type="hidden" name="puntaje_fecha[]" value="value_2">
              <input type="hidden" name="id_calculo[]" value="value_2">
              <input type="hidden" name="puntaje_fecha[]" value="value_3">
              <input type="hidden" name="id_calculo[]" value="value_3">
    

    In your try block through foreach update each value in array.

    try {
    
      $puntaje_fechaArray = $_POST['puntaje_fecha'];
      $id_calculoArray = $_POST['id_calculo'];
    
      foreach ($puntaje_fechaArray as $key => $val){
    
    
        $puntaje_fecha = $val[$key];
        $id_calculo = $id_calculoArray[$key];
    
        $add = "UPDATE calculo SET puntaje_fecha=:puntaje_fecha WHERE id_calculo=:id_calculo";
        $d = $conn->prepare($add);
        $d->bindParam(':puntaje_fecha', $puntaje_fecha );
        $d->bindParam(':id_calculo', $id_calculo);
        $d->execute();
     }
    
    } catch (PDOException $e) {
        die("Error: " . $e->getMessage() . "<br>on line: " . $e->getLine() . "<br>on file: " . $e->getFile());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么