php购物车显示错误

时间:2013-12-28 16:51:32

标签: php shopping-cart

首先,如果之前已经提出这个问题,我很抱歉。我正在购物车。我当前的代码只会在购物车中显示1个商品。这意味着当我点击#product1上的添加到购物车时,它会在购物车中显示#product1详细信息。但是,当我点击#product2的添加到购物车时,它将覆盖#product1。

的信息

这是我的产品代码:

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

以下是process.php的代码:

<?php
 session_start(); 
 include_once("config.php"); 
 $pid=$_POST['product_id'];
 $_SESSION['product'] = array($pid);
 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>";
  else{
  $tblname="products";
  require_once("dbcon.php");
  $proids = array();

  foreach($_SESSION['product'] as $id)
    {
    $proids[] = $id;
    }
   $proids = implode(',', $proids);
   $query = "SELECT * from $tblname where product_id in ('$proids') ";
   $result = mysql_query($query) or die(mysql_error());


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

3 个答案:

答案 0 :(得分:1)

session_start() 添加到 cart.php的开头以恢复会话。

此外,为了向$_SESSION['product']变量添加多个产品ID,这可能就是您要做的事情,您必须更改
$_SESSION['product'] = array($pid);

$_SESSION['product'][] = $pid;

$pid插入$_SESSION['product']数组的末尾。

答案 1 :(得分:0)

USE array_push()。使用卡片ID制作一个数组并使用forloop显示它们

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

尝试

 <?php
session_start();

if(isset($_SESSION['quantity']))
$_SESSION['quantity']=$_SESSION['quantity']+1;
else
$_SESSION['quantity']=1;
echo "Cart=". $_SESSION['quantity'];
?>

答案 2 :(得分:0)

因为你似乎真的坚持这个:

<强> process_cart.php

<?php

 session_start(); 
 include_once("config.php"); 

 // test & generate cart if needed
 if(! isset($_SESSION['cart']))
   {
   $_SESSION['cart'] = array("products"=>array());
   }

// add current product
// ! note: you need to add data validation to this to avoid SQL injects
 $_SESSION['products'][$_POST['product_id']] = $_POST['quantity'];

// Show you have done something
sleep(2);
echo "Add to cart successful";
header("refresh:1;url=cart.php");

exit();
?>

<强> databaseConnection.php

<?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);
?>

<强> cart.php:

<?php
session_start(); 
if (!isset($_SESSION['cart']))
  {
  // No items yet added to the cart
  echo "<p>Your shopping cart is empty!</p>";
  }
else
  {
  // set table
  $tblname = "products";

  // get connection
  require_once('DatabaseConnection.php');

  // create a list with product ID's
  $prodids = array();
  // For each product in the cart
  foreach($_SESSION['cart']['products'] as $id => $qntity)
    {
    // add id to the array
    $prodids[] = $id;
    }

  // combine into a string
  $prodids = implode(',', $prodids);

  $query = "SELECT * from $tblname where product_id in ($prodids) ";

  // (The query should really be mysqli)
  $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();

?>