doubaisui2526 2019-05-15 06: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 07: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部