phparraysmysqliinsertform-submit

Insert row from form submission, get the inserted id, then insert repeatable fields as multiple rows using the new id as a foreign key


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 database comes from variables that are arrays. At this moment when I insert into database, I get the right TIMES of insert, only the values that are inserted (in this case 4 times) are the same values, as in the last row of the HTML form.

I tried implode, even extract values from the VAR, but I'm sure I'm on the wrong track.

if(isset($_GET['submit']))
{    
    $client_id = ($value->ID);
    $qry = "INSERT INTO salesorder (client_id)
            VALUES ('$client_id')";
    $result = mysqli_query($mysql,$qry) or die(mysqli_error($mysql));
    $order_id = mysqli_insert_id($mysql);
    foreach ($results as $result) :  
        $food_id = $_GET['foodid'];
        print_r($food_id);
        $qty = $_GET['qty'];
        $qry="INSERT INTO orderline (order_id, food_id, qty) VALUES ($order_id, '$food_id', '$qty') ";
        $result = mysqli_query($mysql,$qry);
    endforeach; 
}               
<tbody>
    <form action="" method="GET">
        <?php foreach ( $results as $result ) : ?>
            <tr>
                <td><input name="qty" size="2" type="number"></td>
                <td><?php print($result->food_type); ?></td>
                <td><input name="foodid[]" size="4" type="number" value=<?php print($result->food_id); ?>></td>
            <tr>
        <?php endforeach; ?>
</tbody>

This is what the form looks like:

So the form is first dynamic loaded, from the database. could be 2 lines, or 50 lines... but my problem is, after the client fills out qty where he wants to place order off, to "read" the whole form, and load it back into the database


Solution

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