dongpi9494 2015-04-13 09:09
浏览 86
已采纳

php通过循环更新

When users fill in a form they also fill in the number of axles. the information gets send to the table: "train_information" when submitted. The row axles is a FK and also gets send in the table "axle". The table axle looks like this after insert:

database table

Now i want to be able to update the distance (Wich is NULL right now). I do this by showing the number of axles and make a field box of them:

                 <tr> 
                    <?php      
                        foreach($show_axle as $axleposition){ ?>
                        <input type='hidden' name='axle_id' value='<?php echo $axleposition['axle_id']?>'>
                        <td><input type='text' name='distance' id = "<?php echo $axleposition['axle_id']?>"placeholder="<?php echo $axleposition['axle']?>"></td>

                        <?php
                        } 
                    ?>
                </tr>

As you can see i show it in a form. The form action is:

<form method='POST' action='axle_update.php'>

So when they press submit, they go to axle_update.php wich looks like this:

<?php
    ?><pre><?php print_r($_POST) ?> </pre> <?php 

    $update_axle = $database->update_axles();
?>

(The pre is for myself to see what gets send)

This kinda works. becuase when i have for example 12 rows (See image). it only updates the last row. Now this is because the name of the hidden field is the same everywhere. But i don't know how to change that (In the query).

EDIT:

Sorry forgot the update_axles:

function update_axles() {
        $sql = "UPDATE axle SET distance = :distance WHERE axle_id = :axle_id";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(':axle_id', $_POST['axle_id'], PDO::PARAM_INT);
        $sth->bindParam(":distance", $_POST['distance'], PDO::PARAM_STR);
        $sth->execute();
    }
  • 写回答

1条回答 默认 最新

  • douqulv6059 2015-04-13 12:22
    关注

    Your inputs are overwritten every time you add another input.

    To solve this, use an input array. As follows;

    <tr> 
    <?php      
      foreach($show_axle as $axleposition){ ?>
           <input type='hidden' name='axle_id[<?php echo $axleposition['axle_id']?>]' value='<?php echo $axleposition['axle_id']?>'>
           <td><input type='text' name='distance[<?php echo $axleposition['axle_id']?>]' id = "<?php echo $axleposition['axle_id']?>"placeholder="<?php echo $axleposition['axle']?>"></td>
    <?php
      } 
    ?>
    </tr>
    

    Now, when you POST your form, you will have an associative array.

    Now all you have to do is call $database->update_axles() in a loop, passing the ID and the values.

    Note: Depending on how big your input is, don't pass it in to a loop, as querying in a loop is advised against!

    foreach($_POST['axle_id'] as $id) {
        $update_axle = $database->update_axles($id);
    }
    

    And finally, change your method to accept those to parameters, and modify your query.

    function update_axles($id) {
      $sql = "UPDATE axle SET distance = :distance WHERE axle_id = :axle_id";
      $sth = $this->pdo->prepare($sql);
      $sth->bindParam(':axle_id', $id, PDO::PARAM_INT);
      $sth->bindParam(":distance", $_POST['distance'][$id], PDO::PARAM_STR);
      $sth->execute();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题