dongnao2048 2013-06-14 22:05
浏览 12

将商品添加到购物车不工作,看不到任何错误

I'm currently designing a shopping cart following a tutorial online.

I have followed as much as I can down to each detail, with some changes such as variable names etc changing.

I have checked through the code below, however when I clicked "Add To Cart" it doesn't display the added item in the side bar.

Any suggestions as to what the issue may be? (The code is only the segment relating to the sidebar, the products php is in a separate php file.)

<div id="sidebar">

<h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM products WHERE SKU IN("; 
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1) . ") ORDER BY SKU ASC";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
?>
<p><?php echo $row['name']; ?><?php echo $_SESSION['cart'][$row['SKU']]['quantity']; ?></p>
<a href="index.php?page=cart">Go To Cart</a>
<?php
}
}else {
echo "<p>Your part is empty. <br />Please add some products</p>";
}
?>

</div>

Section of code from products PHP file which does the adding:

<?php

if(isset($_GET['action']) && $_GET['action'] == "add"){
$id = $_GET['id'];
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
} else {
$sql2 = "SELECT * FROM products WHERE SKU=$id";
$query2 = mysql_query($sql2);

if(mysql_num_rows($query2) != 0){
$row2 = mysql_fetch_array($qery2);
$_SESSION['cart'][$row2['SKU']] = array("quantity" => 1, "price" => $row2['price']);
} else {
$message = "This product ID is invalid";
}
}
}


?>
  • 写回答

2条回答 默认 最新

  • doumen5491 2013-06-14 22:27
    关注

    You have brackets in your query.

    Change

    $sql2 = "SELECT * FROM products WHERE SKU=[$id]";
    

    to

    $sql2 = "SELECT * FROM products WHERE SKU=$id";
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向