I have a php code to add a product to session, but it does not work as expected, below is my code:
<?php
session_start();
include 'db.php';
$status = 1;
if (isset($_POST['id']) && $_POST['id']!=""){
$id = $_POST['id'];
$sql = "SELECT * FROM website_tree WHERE id = '$id' ";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
$id = $row['id'];
$name = $row['name'];
$price = $row['price'];
$image = $row['image'];
$cartArray = array(
'id'=>$id,
'name'=>$name,
'price'=>$price,
'image'=>$image,
'quantity'=>1
);
if(empty($_SESSION["shopping_cart"]['product'])) {
$_SESSION["shopping_cart"]['product'] = array_push($_SESSION["shopping_cart"], $cartArray);
$status = 1;
}else{
$_SESSION["shopping_cart"]['product'] = array_push($_SESSION["shopping_cart"], $cartArray);
$status = 1;
}
}
echo json_encode(array("status"=>$status));
?>
I get this warning:
array_push() expects parameter 1 to be array, null
Can anyone help me to correct my code?