PHP购物车只列出一个项目

时间:2013-12-28 11:35:24

标签: php shopping-cart

我正在开发一个在线商店项目。在我的代码中,购物车只会列出1个项目。例如,当我点击iphone 5s下的添加到购物车按钮时,它将跳转到购物车并向我显示1个数量的Iphone 5s。然而,当我再次按三星时,它将覆盖Iphone 5s并仅向我显示三星。

以下是产品代码:

<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>

这是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();
 ?>

这是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 个答案:

答案 0 :(得分:1)

$_SESSION['pid']=$_POST['product_id']; - 这可能每次都会替换购物车中的产品。应该是一系列产品。

答案 1 :(得分:1)

编辑:

 $_SESSION['pid']=$_POST['product_id'];
 $_SESSION['qty']+=$_POST['quantity'];

if(! isset($_SESSION['product']))
  {
  $_SESSION['product'] = array();
  }

 $_SESSION['product'][$_POST['product_id']] = $_POST['quantity'];

然后编辑您的处理以循环通过会话&amp;得到每个产品。

编辑:您应该考虑使用$_SESSION['cart']['products'] = array();,因为它在您的vars的组织方面更有意义:Produacts在购物车中,并且作为购物车的一部分。

EDIT2:

这就是我根据使用$ _SESSION ['cart'] ['products']存储产品来组织购物车的方式。注意:未经测试的代码;可能会有一些错别字。只需仔细阅读并尝试理解逻辑:

// Move these into a different file, so you only have it once.
// Then use require_once('DatabaseConnection.php');

define("DB_HOST", "localhost");
define("DB_NAME", "onlinestore");
define("DB_USER", "root");
define("DB_PASSWORD", "");
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);

if (!isset($_SESSION['cart']))
  {
  echo "<p>Your shopping cart is empty!</p>";
  }
else
  {
  $tblname = "products";
  require_once('DatabaseConnection.php');

  $prodids = array();
  foreach($_SESSION['cart']['products'] as $id => $qntity)
    {
    $prodids[] = $id;
    }
  $prodids = implode(',', $prodids);

  $query = "SELECT * from $tblname where product_id in ($prodids) ";
  $result = mysql_query($query) or die(mysql_error());

  echo "<table>";

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    {
    echo "<tr><td>" . $row['product_name'] . "</td><td>" . $row['product_price'] . "</td></tr>". $_SESSION['cart']['products'][$row['product_id']];
    }
    echo "</table>";
    mysql_free_result($result);
    mysql_close();