问:这是我的代码...
<?php
session_start();
$conn=mysqli_connect('localhost','root','...','ecom');
if(isset($_POST['add'])){
if(isset($_SESSION['SC'])){
$item_array_id=array_column($_SESSION['SC'],'item_id');
if(!in_array($_GET['id'],$item_array_id)){
$count=count($_SESSION['SC']);
$item_array=array(
'item_id'=> $_GET['id'],
'item_name'=>$_POST['name'],
'item_price'=>$_POST['pricehi'],
'item_quantity'=>$_POST['qty']
);
$_SESSION['SC'][$count]=$item_array;
}else{
echo'<script>alert(item already added)</script>';
echo'<script>window.location=index.php</script>';
}
}else{
$item_array=array(
'item_id'=> $_GET['id'],
'item_name'=>$_POST['name'],
'item_price'=>$_POST['pricehi'],
'item_quantity'=>$_POST['qty']
);
$_SESSION['SC'][0]=$item;
}
}
if(isset($_GET['action'])){
if($_GET['action']=='delete'){
foreach($_SESSION['SC'] as $keys =>$val){
if($val['item_id']==$_GET['id']){
unset($_SESSION['SC']['$keys']);
echo'<script>alert(item removed)</script>';
echo'<script>window.location=index.php</script>';
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ecom</title>
</head>
<body>
<div class='container'>
<?php
$query='SELECT * FROM cart ';
$result=mysqli_query($conn,$query);
if(mysqli_num_rows($result)>0){
while($row=mysqli_fetch_array($result)){
?>
<div class="col-md-4">
<form method='POST' action="cart.php?actin=add&id=<?php echo $row['id'];?>">
<img src="<?php echo $row['image'];?>" >
<h4 class='text-info'><?php echo $row['title'];?></h4>
<h4 class='text-danger'><?php echo $row['price'];?></h4>
<input type="text" name='qty' class='form-contro' value='1'>
<input type="hidden" name='name'value=<?php echo $row['title'];?>>
<input type="hidden" name='pricehi'value=<?php echo $row['price'];?>>
<input type="submit" class='btn btn-primary' value='add to cart' name='add'>
</form>
</div>
<?php
}
}
?>
</div>
</body>
</html>
这是cart.php代码
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
</head>
<body>
<div class='container'>
<div class='table-responsive'>
<h3>CART</h3>
<table class='table table-bordered'>
<tr>
<th width='40%'>Name</th>
<th width='10%'>Quantity</th>
<th width='20%'>price</th>
<th width='15%'>total</th>
<th width='5%'>Action</th>
</tr>
<?php
if(!empty($_SESSION['SC'])){
$total=0;
foreach($_SESSION['SC'] as $key=>$val){
?>
<tr>
<td><?php echo $val['item_name'];?></td>
<td><?php echo $val['item_quantity'];?></td>
<td><?php echo $val['item_price'];?></td>
<td><?php echo number_format ($val['item_quantity'] * $val['item_price'], 2);?></td>
<td><a href="cart.php=action=delete&id=<?php echo $val['item_id'];?>"><span class='text-danger'>Remove</span></a></td>
</tr>
<?php
$total= $total+($val['item_quantity'] * $val['item_price']);
}
?>
<tr>
<td colspan=3 align='right'>total</td>
<td align='right'><?php echo number_format($total,2);?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
```and the output is persisted with product1
Name Quantity price total Action
a.apple 1 100 100.00 Remove
total 100.00
答:尝试var_dump($ _ SESSION ['SC']); 检查会话变量中真正包含的内容。
问题可能出在您如何将商品添加到购物车中。在显示的第一个代码中将商品添加到购物车的语句之后,执行var_dump($ _ SESSION ['SC']);。并查看是否总是数组中只有一个元素。