I have this php code for inserting data from my from;
$i = 1;
foreach ( $_POST['form'] as $val => $form ){
$Style = $_POST['form'][$i]['style'];
$Dim= $_POST['form'][$i]['Dim'];
$Colour= $_POST['form'][$i]['Colour'];
$Quantity= $_POST['form'][$i]['Quantity'];
$stmt = $db->prepare("INSERT INTO orders(Cus_ID, Style, Dimensions, Colour, Quantity) VALUES(:Cus_ID,:Style,:Dimensions,:Colour,:Quantity)");
$stmt->execute(array(':Cus_ID' => $Cus_ID, ':Style' => $Style, ':Dimensions' => $Dim, ':Colour' => $Colour, ':Quantity' => $Quantity));
$i++;
}
When I submit my form a var_dump give me something like this;
array (size=3)
'form' =>
array (size=3)
1 =>
array (size=4)
'style' => string '0' (length=1)
'Dim' => string '0' (length=1)
'Colour' => string '0' (length=1)
'Quantity' => string '' (length=0)
2 =>
array (size=4)
'style' => string '0' (length=1)
'Dim' => string '0' (length=1)
'Colour' => string '0' (length=1)
'Quantity' => string '' (length=0)
3 =>
array (size=4)
'style' => string '0' (length=1)
'Dim' => string '0' (length=1)
'Colour' => string '0' (length=1)
'Quantity' => string '' (length=0)
'submit' => string 'Place Order' (length=11)
Everything is fine and dandy, but when i delete part of my form the submitted aray will end up something like this;
array (size=2)
'form' =>
array (size=2)
1 =>
array (size=4)
'style' => string '0' (length=1)
'Dim' => string '0' (length=1)
'Colour' => string '0' (length=1)
'Quantity' => string '' (length=0)
3 =>
array (size=4)
'style' => string '0' (length=1)
'Dim' => string '0' (length=1)
'Colour' => string '0' (length=1)
'Quantity' => string '' (length=0)
'submit' => string 'Place Order' (length=11)
To which the php trys to enter the '2nd' array which doesn't exist, is it possible to have the php 'skip/ignore' any arrays that are missing (the array are unlimited and any random number could be missing)
$i? You're doingforeach($_POST['form'] as $key => $form), so just access$formdirectly.if (!isset($_POST['form'][$i])) continue;at the very start inside your loop. This is what you want but it's not what you should be doing. Read up onforeach loop