PHP购物车问题:添加到购物车按钮跳转后商品未显示在表格
问题分析与修复方案
你遇到的页面空白+购物车商品不显示问题,主要是PHP大小写错误、会话未初始化、HTML/JS语法错误这几个核心问题导致的,下面逐一拆解并给出完整修复方案:
核心错误点
- 会话未启动:使用
$_SESSION存储购物车数据前,必须调用session_start()初始化会话,否则无法读写会话数据 - PHP超级全局变量大小写错误:
$_post、$_get是错误写法,PHP中这些变量严格区分大小写,正确写法为$_POST、$_GET - SESSION数组键名不统一:代码里混用了
shopping_cart和shopping_Cart,导致无法正确判断商品是否已存在于购物车 - HTML/JS语法漏洞:
- img标签未闭合,缺少
/> - CSS里
bacgkround-color拼写错误、border:1px solid未指定颜色 - 删除按钮的a标签未闭合,缺少闭合
> - JS跳转代码存在语法错误(缺少引号和闭合标签)
- 表格里
coslpan是拼写错误,正确为colspan
- img标签未闭合,缺少
修正后的完整代码
<?php // 必须先初始化会话,放在所有输出之前 session_start(); require "includes/dbh.php"; if(isset($_POST['add'])){ if(isset($_SESSION['shopping_cart'])){ // 统一SESSION数组键名,避免大小写问题 $item_array_id = array_column($_SESSION['shopping_cart'],'item_id'); if(!in_array($_GET['id'], $item_array_id)){ $count = count($_SESSION['shopping_cart']); $item_array = array( 'item_id' => $_GET['id'], 'item_name' => $_POST['hidden_name'], 'item_price' => $_POST['hidden_price'], 'item_quantity' => $_POST['quantity'], ); $_SESSION['shopping_cart'][$count] = $item_array; }else{ echo "<script>alert('Item already added');</script>"; echo "<script>window.location='displayitems.php';</script>"; } }else{ $item_array = array( 'item_id' => $_GET['id'], 'item_name' => $_POST['hidden_name'], 'item_price' => $_POST['hidden_price'], 'item_quantity' => $_POST['quantity'], ); $_SESSION['shopping_cart'][0] = $item_array; } } if(isset($_GET['action'])){ if($_GET['action']=='delete'){ // 先判断购物车是否存在,避免报错 if(isset($_SESSION['shopping_cart'])){ foreach($_SESSION['shopping_cart'] as $keys => $values){ if($values['item_id'] == $_GET['id']){ unset($_SESSION['shopping_cart'][$keys]); echo '<script>alert("Item removed");</script>'; echo '<script>window.location="displayitems.php";</script>'; } } } } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Page Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> <script src="main.js"></script> <style> .product{ border:1px solid #ccc; /* 补充边框颜色 */ margin:-1px 19px 3px -1px; padding: 10px; text-align:center; background-color:#efefef; /* 修正拼写错误 */ } </style> </head> <body> <?php if(isset($_SESSION['userId'])){ echo '<div id="items" >'; include_once 'includes/dbh.php'; require 'includes/gallery-upload.in.php'; $sql="SELECT * FROM gallery ORDER BY orderitems DESC;"; $stmt=mysqli_stmt_init($conn); if(!mysqli_stmt_prepare($stmt,$sql)){ echo "SQL statement failed in displayitems.php"; }else{ mysqli_stmt_execute($stmt); $result=mysqli_stmt_get_result($stmt); while($row=mysqli_fetch_assoc($result)) { ?> <!-- 添加到购物车按钮的表单 --> <div class="col-md-3"> <form method="POST" action="displayitems.php?action=add&id=<?php echo $row["idGallery"]?>"> <div class="product"> <!-- 闭合img标签 --> <img src="images/<?php echo $row['imgFullNameGallery'] ?>" /> <br> <h3><?php echo $row['nameitem']?></h3> <h3><?php echo $row['price']?></h3> <!-- 增加min属性防止输入负数 --> <input type='number' name='quantity' value="1" min="1"> <input type='hidden' name='hidden_name' value="<?php echo $row['nameitem']?>"> <input type='hidden' name='hidden_price' value="<?php echo $row['price']?>"> <button type='submit' name='add'>Add to cart</button> </div> </form> </div> <?php } } } ?> <div style="clear:both"></div> <!-- 修正冒号位置 --> <h3 class="title2">Shopping cart details</h3> <div class="table table-bordered"> <table> <tr> <th width="40%">Item 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['shopping_cart'])){ $total=0; foreach($_SESSION['shopping_cart'] as $keys => $values){ ?> <tr> <td><?php echo $values['item_name'];?></td> <td><?php echo $values['item_quantity'];?></td> <td>$<?php echo $values['item_price'];?></td> <td><?php echo number_format($values['item_quantity']*$values['item_price'],2);?></td> <!-- 修复a标签闭合问题 --> <td><a href="displayitems.php?action=delete&id=<?php echo $values['item_id']; ?>" class="text-danger">REMOVE</a></td> </tr> <?php $total += ($values['item_quantity']*$values['item_price']); } ?> <tr> <!-- 修复colspan拼写错误 --> <td colspan="3" align="right">Total</td> <td align="right">$<?php echo number_format($total,2);?></td> <td></td> <!-- 补充空单元格对齐 --> </tr> <?php } else { // 购物车为空时增加提示 echo '<tr><td colspan="5" align="center">Your shopping cart is empty</td></tr>'; } ?> </table> </div> </body> </html>
额外优化建议
- 对用户输入的
quantity进行合法性校验(比如是否为正整数) - 建议使用预处理语句处理
$_GET['id'],避免SQL注入风险 - 可以添加购物车商品数量的实时更新提示,提升用户体验
内容的提问来源于stack exchange,提问作者Jenel Hapitana
相关产品推荐
相关产品推荐

