I have a HTML form that i populate from database with a "foreach" loop, so the fields in the name have the same name. The data that i want to post back into the databa
Am not sure what your HTML looks like but having the same name for all your inputs does not neccessarily mean they have become an array. you will need to define them as array in HTML using []
. Below is an example
HTML
<form method="post" name="myform">
<input type="text" name="array[]" Value="101"/>
<input type="text" name="array[]" Value="102"/>
<input type="text" name="array[]" Value="103"/>
<input type="text" name="array[]" Value="104"/>
<input type="submit" name="submit" Value="submit"/>
</form>
PHP
if(isset($_POST['submit'])){
foreach($_POST['array'] as $myarray) {
echo $myarray.'<br>';
}
this should get you the unique values from each input and then you do what you want with it.
Here is a link to several other solutions that could help: Post text box array in PHP
If you are getting an array from your HTML form then you need to loop on this array and insert each row separately into DB. To do this you need to use prepared statement and a loop.
if (isset($_GET['submit'])) {
$client_id = $value->ID; // Wherever this value comes from...
// Insert new sales order
$stmt = $mysql->prepare('INSERT INTO salesorder (client_id) VALUES (?)');
$stmt->bind_param('s', $client_id);
$stmt->execute();
$stmt->store_result();
$order_id = $mysql->insert_id;
// prepare the SQL statement
$orderline_stmt = $mysql->prepare('INSERT INTO orderline (order_id, food_id, qty) VALUES (?,?,?)');
// loop on each element from HTML form
// i.e. <input name="foodid[]" >
foreach ($_GET['foodid'] as $key => $food_id) {
$qty = $_GET['qty']; // should this be an array too?
// $qty = $_GET['qty'][$key]; <-- if it's also an array
$orderline_stmt->bind_param('sss', $order_id, $food_id, $qty);
$orderline_stmt->execute();
}
}