【发布时间】:2016-06-15 06:13:23
【问题描述】:
这是我的购物车代码:
function cart() {
$total = 0;
$item_quantity = 0;
$item_name = 0;
$item_number = 1;
$amount = 1;
$quantity = 1;
foreach ($_SESSION as $name => $value) {
if($value > 0 ) {
if(substr($name, 0, 8) == "product_"){
$length = strlen($name - 8);
$id = substr($name, 8 , $length);
$query = query("SELECT * FROM products WHERE product_id = " . escape_string($id)." ");
confirm($query);
$titleArr = array();
while($row = fetch_array($query)) {
$titleArr[$id] = $row['product_title'];
$product_name = implode(",", $titleArr);
$order_product = $product_name . "-". $value;
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$item_number++;
$amount++;
$quantity++;
$id++;
}
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
$_SESSION['item_name'] = $order_product;
}
}
}
}
我在网上找了很久,请指教。
$_SESSION['item_name'] = $order_product;
它给出了 fetch_array 的最后一个值我想要购物车页面上的所有产品标题。
【问题讨论】:
-
因为这段代码会覆盖你的会话数据。您应该必须使用 $_SESSION['item_name'][] = $order_product;
-
致命错误:字符串不支持 [] 运算符
标签: php mysql sql database php-5.5