我有一个 php 代码将产品添加到会话中,但它没有按预期工作,下面是我的代码:
<?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));
?>
我收到这个警告: array_push() expects parameter 1 to be array, null
谁能帮我纠正我的代码?
达令说
噜噜哒