duannaoben8011 2013-12-28 11:35 采纳率: 0%
浏览 16

PHP购物车只列出一个项目

I am working on an onlinestore project. In my code,the shopping cart will only list 1 item. For example when i click add to cart button under iphone 5s it will jump to the cart and show me Iphone 5s with 1 quantity. However, when i press again Samsung, it will overwrite Iphone 5s and show me only Samsung.

Here is the code for product:

<div>
    <image src="ip5s.jpg">
    <p><font color="blue">&nbsp&nbspIphone 5S</font></p>
    <p><font color="red">&nbsp&nbspRM1999</font></p>
    <p><form name="addcart" method="post" action="processcart.php">
        <input type="submit" name="addtocart" value="Add to cart" >
        <input type="hidden" name="product_id" value="1234" />
        <input type="hidden" name="quantity" value="1" />
    </form>
</p>
</div>
&nbsp&nbsp
<div >
    <image src="s4.png">
    <p><font color="blue">&nbsp&nbspSamsung Galaxy S4</font></p>
    <p><font color="red">&nbsp&nbspRM1999</font></p>
    <p><form name="addcart" method="post" action="processcart.php" >
        <input type="submit" name="addtocart" value="Add to cart">
        <input type="hidden" name="product_id" value="1235" />
        <input type="hidden" name="quantity" value="1" />
    </form>
</p>
</div>

Here is the processcart.php:

<?php
 session_start(); 
 include_once("config.php"); 
 $_SESSION['pid']=$_POST['product_id'];
 $_SESSION['qty']+=$_POST['quantity'];
 $_SESSION['cart']=true;
 sleep(2);
 echo "Add to cart successful";
 header("refresh:1;url=cart.php");
 exit();
 ?>

Here is the cart.php:

<?php

if (!isset($_SESSION['cart']))
    echo "<p>Your shopping cart is empty!</p>";
elseif (isset($_SESSION['cart'])) {
    define("DB_HOST", "localhost");
    define("DB_NAME", "onlinestore");
    define("DB_USER", "root");
    define("DB_PASSWORD", "");
    $tblname = "products";
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_NAME);
    $proid = $_SESSION['pid'];
    $query = "SELECT * from $tblname where product_id='$proid'";
    $result = mysql_query($query);
    $count = mysql_num_rows($result);
    echo "<table>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "<tr><td>" . $row['product_name'] . "</td><td>" . $row['product_price'] . "</td></tr>";
        echo $_SESSION['qty'];
    }
    echo "</table>";
    mysql_free_result($result);
    mysql_close();
}
?>
  • 写回答

2条回答 默认 最新

  • dozabt4329 2013-12-28 11:37
    关注

    $_SESSION['pid']=$_POST['product_id']; - this probably each time replaces product in the cart. Should be an array of products.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100