我正在使用 PHP 创建一个网上商店并使用会话来保存临时项目。
<form method="post" action="basket.php">
<table border="1" style="width:400px;">
<tr>
<td style="width:35%;">Product</td>
<td style="width:30%;">Aantal</td>
<td style="width:25%;">Totale prijs</td>
<td style="width:10%;"></td>
</tr>
<?php
for($i = 0; $i < count($_SESSION['basket']); $i++) {
$id = $_SESSION['basket'][$i]['itemId'];
$number = $_SESSION['basket'][$i]['number'];
$result = $mysqli->query('SELECT * FROM items WHERE id = "' . $id . '"');
$row = $result->fetch_assoc();
$total = $number * $row['price'];
?>
<tr>
<td>
<a href="view_item.php?id=<?php echo $id; ?>">
<?php echo $row['name']; ?>
</a>
</td>
<td><?php echo $number; ?></td>
<td>€ <?php echo $total; ?></td>
<td><input type="submit" name="removeItem[]" value="Verwijder" /></td>
</tr>
<?php
if (isset($_POST['removeItem'])) {
unset($_SESSION['basket'][$i]);
}
$_SESSION['basket'] = array_values($_SESSION['basket']);
}
?>
</table>
</form>
当我在会话中设置 3 个项目时。
Array
(
[basket] => Array
(
[0] => Array
(
[itemId] => 1
[number] => 1
[timestamp] => 1380722942
)
[1] => Array
(
[itemId] => 1
[number] => 1
[timestamp] => 1380722944
)
[2] => Array
(
[itemId] => 1
[number] => 1
[timestamp] => 1380722945
)
)
)
并删除第二个项目,它也会删除最后一个项目..
出了什么问题?
有人可以帮助我吗?