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();
    }

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();
}