Der undefinierte Array-Schlüssel „Menge“ erscheint in C:\xampp\htdocs\login\cart-item.php
<p>Ich folge dem Tutorial-Video, aber es gibt keine Ausgabetabelle in der Datenbank. Ich habe jeden Schritt im Video verfolgt.我还尝试将数量初始化为变量,但仍然无法工作</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'],
"Preis" => $_POST['Preis'],
"Menge" => $_POST['Menge']
);
$_SESSION['cart'][] = $session_array;
}
}anders{
$session_array = array(
'id' => $_GET['id'],
"name" => $_POST['name'],
"Preis" => $_POST['Preis'],
"Menge" => $_POST['Menge']
);
$_SESSION['cart'][] = $session_array;
}
}?>
<!DOCTYPE html>
<html>
<Kopf>
<title>Produkte</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="stylesheet" type="text/css" href="cart-item.css">
<Stil>
</style>
</head>
<Körper>
<div class="container-fluid">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">购物车数据</h2>
<div class="col-md-12">
<div class="row">
<?php
$query = "SELECT * FROM cart_item";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)){
?>
<div class="col-md-4">
<form method="post" action="cart-item.php?id=<?= $row['id'] ?>">
<h5 class="text-center"><?= $row['name']; ?></h5>
<h5 class="text-center">$<?= number_format($row['price'], 2); ?></h5>
<input type="hidden" name="name" value="<?= $row['name'] ?>">
<input type="hidden" name="price" value="<?= $row['price'] ?>">
<input type="number" name="quantity" value="1" class="form-control">
<input type="submit" name="add_to_cart" class="btn btn-warning btn-block my-2" value="添加到购物车">
</form>
</div>
<?php
}
?>
</div>
</div>
</div>
<div class="col-md-6">
<h2 class="text-center">Ausgewählte Elemente</h2>
<?php
$total = 0;
$output = "";
$output .= "
<table class='table table-bordered table-striped'>
<tr>
<th>ID</th>
<th>Produktname</th>
<th>Produktpreis</th>
<th>Artikelmenge</th>
<th>Gesamtpreis</th>
<th>Operation</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'>Entfernen</button>
</a>
</td>
</tr>
";
$total = $total + $value['quantity'] * $value['price'];
}$output .= "
<tr>
<td colspan='3'></td>
<td></b>Gesamtpreis</b></td>
<td>".number_format($total, 2)."</td>
<td>
<a href='cart-item.php?action=clearall'>
<button class='btn btn-warning btn-block'>Clear</button>
</a>
</td>
</tr>
";
}
echo $output;
?>
</div>
</div>
</div>
</div>
</body>
</html></pre>
<p>Ich habe den Mengenarray-Schlüssel mehrmals überprüft und mit dem Video verglichen, genau wie im Video. Gibt es noch etwas, das ich versuchen sollte? Die Tabelle in der Datenbank enthält auch nicht die Menge</p>