未定義的陣列鍵"quantity"出現在C:\xampp\htdocs\login\cart-item.php中
<p>我正在按照教學影片操作,但是資料庫中沒有輸出表格。我按照影片中的每一步操作。我還嘗試將數量初始化為變量,但仍然無法工作</p>
<pre class="brush:php;toolbar:false;"><?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "login_sample_db");
if(isset($_POST['add_to_cart'])){
if(isset($_SESSION['cart'])){
$session_array_id = array_column($_SESSION['cart'], "id");
if(!in_array($_GET['id'], $session_array_id)){
$session_array = array(
'id' => $_GET['id'],
"name" => $_POST['name'],
"price" => $_POST['price'],
"quantity" => $_POST['quantity']
);
$_SESSION['cart'][] = $session_array;
}
}else{
$session_array = array(
'id' => $_GET['id'],
"name" => $_POST['name'],
"price" => $_POST['price'],
"quantity" => $_POST['quantity']
);
$_SESSION['cart'][] = $session_array;
}
}?>
<頭>
<標題>產品標題>
<風格>
</風格>
</頭>
<正文>
<div class="container-fluid">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
購物車數據
<div class="col-md-12">
<div class="row">
">;
= $row['name']; ?></h5>
$= number_format($row['price'], 2); ?></h5>
;
</表格>
;
<div class="col-md-6">
<h2 class="text-center">已選擇的商品</h2>
<?php
$total = 0;
$output = "";
$output .= "
<table class='table table-bordered table-striped'>
<tr>
<th>ID</th>
<th>商品名稱</th>
<th>商品價格</th>
<th>商品數量</th>
<th>總價格</th>
<th>操作</th>
</tr>
";
if(!empty($_SESSION['cart'])){
foreach($_SESSION['cart'] as $key => $value){
$output .= "
<tr>
<td>".$value['id']."</td>
<td>".$value['name']."</td>
<td>".$value['price']."</td>
<td>".$value['quantity']."</td>
<td>$".number_format($value['price'] * $value['quantity'])."</td>
<td>
<a href='cart-item.php?action=remove&id=".$value['id']."'>
<button class='btn btn-danger btn-block'>移除</button>
</a>
</td>
</tr>
";
$total = $total $value['quantity'] * $value['price'];
}
$output .= "
<tr>
<td colspan='3'></td>
<td></b>總價</b></td>
<td>".number_format($total, 2)."</td>
<td>
<a href='cart-item.php?action=clearall'>
<button class='btn btn-warning btn-block'>清空</button>
</a>
</td>
</tr>
";
}echo $output;
?>
</div>
</div>
</div>
</div>
</body>
</html></pre>
<p>我多次檢查了數量數組鍵並與視頻進行比較,與視頻中的相同。我還應該嘗試什麼其他的東西嗎?資料庫中的表格也沒有包含數量</p>