The reason you see duplicates for each parameter group is because your $queryValue2 is repeated for each parameter group inside your outer for($i = 0; $i < $itemCountz; $i++) loop for($i = 0; $i < $itemCountz; $i++) .
In its current form, your current way of organizing $_POST inputs is pretty dirty (and your duplicate string attributes). I suggest you group the HTML input fields for each parameter group. You can try something like this:
<div> <input type="text" id="first_order" name="orders[0][name]" /><br /> <input type="text" name="orders[0][options][0][price]" /><br /> <input type="text" name="orders[0][options][0][size]" /><br /> <input type="text" name="orders[0][options][1][price]" /><br /> <input type="text" name="orders[0][options][1][size]" /><br /> </div> <div> <input type="text" id="second_order" name="orders[1][name]" /><br /> <input type="text" name="orders[1][options][0][price]" /><br /> <input type="text" name="orders[1][options][0][size]" /><br /> <input type="text" name="orders[1][options][1][price]" /><br /> <input type="text" name="orders[1][options][1][size]" /><br /> </div>
Notice how I use the HTML name attribute to group food parameters. Then, if you submit this form, you will see that the $_POST array looks something like this:
Array ( [orders] => Array ( [0] => Array ( [name] => "Cheese Pizza" [options] => [0] => Array([size] => "8'" [price] => "12") [1] => Array([size] => "12'" [price] => "14") ) [1] => Array ( [name] => "Sausage Pizza" [options] => [0] => Array([size] => "8'" [price] => "13") [1] => Array([size] => "12'" [price] => "16") ) ) )
Now you can clean your internal PHP codes with something like this:
foreach ($_POST['orders'] as $orders) { // .. some codes foreach($orders as $order) { // .. add codes to insert into op_groups where $order['name'] will give you the pizza name foreach($order['options'] as $details) { // .. add codes to insert into options where $details['size'] and $details['price'] will give you the corresponding values } } }
Alternatively, you can use mysqli.insert-id to get the row id of the last row inserted. Then you can get rid of this stupid $query6 and its while .
Of course, don't forget to sanitize your $_POST entries before using them!
I get a little tired, if there are errors, I will fix them tomorrow. Hope this helps!
source share