购物车脚本无法正常工作

时间:2014-03-02 22:13:58

标签: php mysql

/* Product List */
$product= mysql_query("select * from product order by p_id asc");
$query = mysql_num_rows($product);
if($query>0)
{
    while($item = mysql_fetch_array($product))
    {
        extract($item);
        echo'<div style="padding:10px;margin-bottom:10px;border-bottom:1px solid #000;">';
        echo'<h1>'.$productName.'</h1>';
        echo'<p>'.$desc.'</p>';
        $result = mysql_query("select * from productOption where p_id = '$p_id' order by option_id asc");
        while($sql = mysql_fetch_array($result))
        {
            extract($sql);
            echo'<a href="?action=id='.$option_id.'">'.$price.'&nbsp;&euro;&nbsp;</a>';
        }
        echo'</div>';
    }
}
else
{
    echo'No items!';
}
/* add to cart */
if(isset($_GET['action']))
{
    $id = $_GET['id'];
    if(isset($_SESSION['cart'][$id]))
    {
        $_SESSION['cart'][$id]++;
    }
    else
    {
        $_SESSION['cart'][$id]=1;
    }
    header('Location:'.$_SERVER['HTTP_REFERER']);
}

/* remove */
if(isset($_GET['remove']))
{
    $id = $_GET['cikart'];
    $_SESSION['cart'][$id]--;
    if($_SESSION['cart'][$id] == 0)
    {
        unset($_SESSION['cart'][$id]);
    }
    header('Location:'.$_SERVER['HTTP_REFERER']);
}

/* empty */
if(isset($_GET['empty']))
{
    unset($_SESSION['cart']);
    header('Location:'.$_SERVER['HTTP_REFERER']);
}

这是我的product.php页面,这是我的购物车脚本。问题是这个脚本不起作用。当我将一个产品添加到我的购物车时,产品ID,产品名称和产品价格来自另一个产品。我的错是什么?请有人帮帮我吗?

<?php
if(isset($_SESSION['cart']))
{
    foreach ($_SESSION['cart'] as $key => $value)
    {
        $query = mysql_query("SELECT * FROM product WHERE p_id = '$key' LIMIT 1");
        while($row = mysql_fetch_array($query))
        {
            extract($row);
            echo'<h2>'.$p_id.'</h2>';
            echo'<p>'.$productName.'</p>';
            $sql = mysql_query("SELECT * FROM productOption WHERE option_id = '$p_id'");
            while($result = mysql_fetch_array($sql))
            {
                extract($result);
                echo '<p>'.$price.'</p>';
            }
        }
    }
    echo '<a href="?empty='.$key.'">Empty</a>';
}
else
{
    echo count($_SESSION['cart']);
}
?>

0 个答案:

没有答案