Undefined array key 'quantity' appears in C:\xampp\htdocs\login\cart-item.php
P粉549412038
P粉549412038 2023-09-05 20:18:59
0
1
623
<p>I am following the tutorial video but there is no output table in the database. I followed every step in the video.I also tried initializing the quantity into a variable but it still doesn't work</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; } }?> <!DOCTYPE html> <html> <head> <title>Products</title> <link rel="stylesheet" type="text/css" href="styles.css"> <link rel="stylesheet" type="text/css" href="cart-item.css"> <style> </style> </head> <body> <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">Selected items</h2> <?php $total = 0; $output = ""; $output .= " <table class='table table-bordered table-striped'> <tr> <th>ID</th> <th>Product Name</th> <th>Product price</th> <th>Item quantity</th> <th>Total Price</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'>Remove</button> </a> </td> </tr> "; $total = $total $value['quantity'] * $value['price']; } $output .= " <tr> <td colspan='3'></td> <td></b>Total Price</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>I checked the quantity array key multiple times and compared with the video, same as in the video. Is there anything else I should try? The table in the database also does not contain quantities</p>
P粉549412038
P粉549412038

reply all(1)
P粉132730839

I'll comment if I know more details, but are you sure your database schema and table definitions are correct? Where is your INSERT/UPDATE logic? Where did the error occur and what exactly was it? Please provide more information.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template