The problem appears when I add a product, the product goes to the cart just fine, but when I add another, it owerwrites the first one, so only the first position of the array. Also I’m using sticky forms, but when I use another add_to_cart submit, the sticky data disappears, firstname etc.
But array_push should add the data to the end of the array?
Also, this is the global scope but when I try to use this data inside a function it disappears. SESSION is superglobal, so I don’t need to include it in global;
function order(){
....
for ($i = 0, $counter = count($_SESSION['cart']); $i <= $counter; $i++) {
$query5 = "INSERT INTO order_items (order_id, product_id, quantity) VALUES ('".$order_id."', '".$_SESSION['cart'][$i][4]."', '".$_SESSION['cart'][$i][3]."')";
mysqli_query($dbc, $query5) or die('MySQL Error order_item: '.mysqli_error($dbc).' ('.mysqli_errno($dbc).')');
}
mysqli_close($dbc);
}
wouldnt using & make PHP think that the variables in the get array were $_GET[‘cart’] and $_GET[‘amp;index’], thus causing it to misfire on which index to remove?
// This sets the links for every product in cart.
echo'<tr> <td>'.$_SESSION['cart'][$i][0].'</td> <td>'.$_SESSION['cart'][$i][1].' EUR</td> <td>'.$_SESSION['cart'][$i][3].'</td>
<td>'.$sum.' EUR</td> <td><a href="shop.php?cart=remove&index='.$i.'">Remove</a></td> </tr>';
// This should remove the data from cart.
if($_GET['cart']=='remove'){
$key = (int)$_GET['index'];
form_display();
unset($_SESSION['cart'][$key]);
header('Location: shop.php');
}
The header function doesn’t even run.
It seems to work, but it only removes the last elements and when removing from the beginning of the array it doesn’t seem to work.