PHP fetch and update N amount of POST

55 Views Asked by At

In advance, I would like to apologize if this topic has been covered.

I am working on a simple PHP project, where I have to set permissions of certain client to certain user ID. I.e Individual user will be able to access only certain clients.

The list of client is in mysql table, and no of rows will be increasing.

So, I have to fetch all rows from table "clients" and to list them on the page. Then I have to update "client_user" for permisisons. How can I update these since when I fetch data, I am not sure how to name HTML input elements so that when I update, I know which elements to fetch?

after update button is clicked:

$_POST['x'];

The x is HTML element. How can I give it ID or any refrence when fetching, so that I can use it when updating?

Many Thanks

enter image description here

enter image description here

1

There are 1 best solutions below

0
DarkBee On

You can use the array name notation in order to figure out which client's information changed

<form method="POST">
<?php
    $clients = getClients();
    foreach($cients as $client) {
?>
   <input type="text" name="name_client[<?= $client->getId(); ?>]" value="<?= $client->getName(); ?>" />
<?php } ?>
</form>

This will generate an array when posted, e.g.

$_POST['name_client'] = [
    1 => 'foo',
    2 => 'bar',
    3 => 'foobar',
];

This array you then can use to modify the correct client's name

<?php
    foreach($_POST['name_client'] as $client_id => $name) {
       //add code to update client with id $client_id
    }
?>