How to correctly record data from a loop in dB?

I deduce the table through the loop and try to make it available for editing. Here is the table itself (easier to understand):
<input type="hidden" name="form_id" value="<?=$users['id']?>">
<table class="table text-center">
<thead class="text-uppercase">
the <tr>
<th scope="col">Metro</th>
<th scope="col">Time</th>
<th scope="col">Room</th>
<th scope="col">Option</th>
$post = get_delivery_users();
<?php foreach ($post as $users): ?>
the <tr>
<th scope="row">1</th>
<td><select name="delivery_adress" class="form-control">
<? foreach(get_delivery_addresses() as $delivery_address): ?>
<option <?if ($delivery_address == $users['delivery_adress']) echo 'selected'?>><?=htmlspecialchars($delivery_address, ENT_QUOTES)?></option>
<? endforeach;?>
<td><input class="form-control" type="text" name="delivery_time" value="<?=htmlspecialchars($users['delivery_time'], ENT_QUOTES)?>" id="example-text-input"></td>
<td><input class="form-control" type="text" name="phone" value="<?=htmlspecialchars($users['phone'], ENT_QUOTES)?>" id="example-text-input"></td>
<td><select name="courier" class="form-control">
<? foreach(get_couriers() as $couriers): ?>
<option <?if ($couriers == $users['courier']) echo 'selected'?>><?=htmlspecialchars($couriers, ENT_QUOTES)?></option>
<? endforeach;?>
<?php endforeach; ?>
<button type="submit" formmethod="post" name="couriers" formaction="courier.php" class="btn btn-xs btn-warning mb-3">Send</button>

Here is courier.php:


 $form_id = (int)$_POST['form_id'];
 $phone = strip_tags(trim($_POST['phone']));
 $delivery_adress = strip_tags(trim($_POST['delivery_adress']));
 $courier = strip_tags(trim($_POST['courier']));
 $SQL = "UPDATE Users SET phone='$phone', delivery_adress='$delivery_adress', courier='$courier' WHERE id='$form_id'";
 $result = mysqli_query($link, $SQL);

 if ($result) {
 else {
 printf("Error: %s\n", mysqli_error($link));

Can't understand why nothing is written to the database. Help with advice on what I'm doing wrong. It turns out that I have multiple rows in table with different id, need to save them all...
March 19th 20 at 09:27
1 answer
March 19th 20 at 09:29
Multiple updates you have to do will not work because your form sends only the data inserted in the last iteration. That is, if you write

$names = ['Bob', 'Peter', 'nick'];
foreach($names as $name):
 echo '<input type="text" name="name" value="' . $name . '">';

Then you have printed
<input type="text" name="name" value="Bob">
<input type="text" name="name" value="Peter">
<input type="text" name="name" value="nick">

The problem is that the name specified in the name attribute must be unique, and here it zadublirovanny 3 times, so as soon as you click "submit" you will use only the latter, i.e. the "nick." It will be sent to the server.

In order to send the entire dataset, you need to either make the values of the name attribute is unique. The easiest way to do it, defining it as an array.

$names = ['Bob', 'Peter', 'nick'];
foreach($names as $key => $name):
 echo '<input type="text" name="names[' . $key . ']" value="' . $name . '">';

And thus code is generated
<input type="text" name="names[0]" value="Bob">
<input type="text" name="names[1]" value="Peter">
<input type="text" name="names[2]" value="nick">

And thus you can transfer the entire data array in on.

Find more questions by tags MySQLPHP