I'm currently making a session shopping cart where I can add and delete products with a link underneath every product. You can check it out here if you prefer it visualized: http://www.bgc-testomgeving.nl/sem
Anyway, somehow when I click the link of the product nothing seems to happen except from the URL changing, but the adding to the $_SESSION['cart'] doesn't seem to occur.
This is the code of the link underneath every product to add it to the cart: echo '<a href="action=add&id=' .$id. '">Voeg toe </a>';
With $id being: $id = get_the_ID(); from WordPress query. This part works however because the URL of the <a> is right I think.
Here I check if there is already a $_SESSION['cart'] active, if not I make one. Also, I check if the item ID is already in cart, if not, I do the array_push() and if so, I use unset. See underneath:
<?php session_start();
require("dbconnect.php");
?>
<?php
if(!isset($_SESSION['cart'])) {
$cart = array();
$_SESSION['cart'] = $cart;
}
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(in_array($id, $_SESSION['cart'])){
if (($key = array_search($id, $_SESSION['cart'] !== false))){
unset($_SESSION['cart'][$key]);
}
}
else {
array_push($_SESSION['cart'],$id);
}
} ?>
I print the array of the $_SESSION['cart'] plus the session_id() on the page to check if it worked with the following code in index.php:
<?php print "Test session number: ";
echo session_id();
echo '<br>';
echo serialize($_SESSION['cart']);
?>
So my question is, why is the $id not pushed to the $_SESSION['cart']
EDIT: I FOUND THE PROBLEM, I FORGOT TO ADD A ? TO THE ARRAY SO THE $_GET didn't know it was a parameter