Php 我无法将多个对象添加到$u会话[';car';]数组中
所以我试着做一个卖酒的基本推车。但由于某些原因,每次我尝试向购物车添加新产品时,我的$u会话['cart']数组会自动重置,而我的购物车只显示我添加到$u会话['cart']数组的最后一个对象 以下是所有事情发生的代码:Php 我无法将多个对象添加到$u会话[';car';]数组中,php,session,Php,Session,所以我试着做一个卖酒的基本推车。但由于某些原因,每次我尝试向购物车添加新产品时,我的$u会话['cart']数组会自动重置,而我的购物车只显示我添加到$u会话['cart']数组的最后一个对象 以下是所有事情发生的代码: try { $validator = new GUMP(); $_GET = $validator->sanitize($_GET); $validation_rules = array( 'id' => 'require
try {
$validator = new GUMP();
$_GET = $validator->sanitize($_GET);
$validation_rules = array(
'id' => 'required|integer|min_numeric,1'
);
$filter_rules = array(
'id' => 'trim|sanitize_numbers'
);
$validator->validation_rules($validation_rules);
$validator->filter_rules($filter_rules);
$validated_data = $validator->run($_GET);
if ($validated_data === false) {
$errors = $validator->get_errors_array();
throw new Exception("Invalid wine id: " . $errors['id']);
}
$id = $validated_data['id'];
$wine =Wine::find($id);
session_start();
$_SESSION['cart'] = array($wine);
if(!in_array($wine, $_SESSION['cart'])){
array_push($_SESSION['cart'], $wine);
$_SESSION['message'] = 'Product added to cart';
}
else{
$_SESSION['message'] = 'Product already in cart';
}
//dd( $_SESSION['cart']);
header("Location: cart.php");
} catch (Exception $ex) {
die($ex->getMessage());
}
提前感谢您的帮助我会:
$_SESSION['cart'][] = $wine;
不
$\u会话['cart']=array($wine)
在会话\u start()之后重置数组。
$wine是否导出对象?只允许使用简单的数据类型。您可以序列化它并在读取时反序列化。