0

我有一个myList.php应该列出添加到我的收藏夹中的所有产品并计算产品总价的列表。

这是代码:

<?php     
    include 'navigation.php' 
?>

<div class='sectionContents'>
<?php
    if (isset($_GET['action']) && $_GET['action'] == 'removed') {
        echo "<div>" . $_GET['prod_name'] . " was removed from favourites.</div>";
    }

    if (isset($_SESSION['fav'])) {
        $ids = "";
        foreach($_SESSION['fav'] as $prod_id) {
            $ids = $ids . $prod_id . ",";
        }

        // remove the last comma
        $ids = rtrim($ids, ',');

        include "db_connect.php";

        $query = mysql_query("SELECT prod_id, prod_name, prod_price FROM tbl_product WHERE    prod_id IN ('$ids')") or die(mysql_error());

        $num = mysql_num_rows($query);

        if ($num > 0) {
            echo "<table border='0'>"; //start table

            // our table heading
            echo "<tr>";
            echo "<th class='textAlignLeft'>Product Name</th>";
            echo "<th>Price (MUR)</th>";
            echo "<th>Action</th>";
            echo "</tr>";

            //also compute for total price
            $totalPrice = 0;

            while ($row = mysql_fetch_assoc($query)) {
                extract($row);

                $totalPrice += $prod_price;

                //creating new table row per record
                echo "<tr>";
                echo "<td>{$prod_name}</td>";
                echo "<td class='textAlignRight'>{$prod_price}</td>";
                echo "<td class='textAlignCenter'>";
                echo "<a href='remove_favourite.php?prod_id=   {$prod_id}&prod_name={$prod_name}' class='customButton'>";
                echo "<img src='shopping-cart-in-php/images/remove-from-   cart.png' title='Remove from favourite' />";
                echo "</a>";
                echo "</td>";
                echo "</tr>";
            }

            echo "<tr>";
            echo "<th class='textAlignCenter'>Total Price</th>";
            echo "<th class='textAlignRight'>{$totalPrice}</th>";
            echo "<th></th>";
            echo "</tr>";

            echo "</table>";
            echo "<br /><div><a href='#' class='customButton'>Home</a></div>";

        } else {
            echo "<div>No products found in your favourites. :(</div>";
        }
    } else {
        echo "<div>No products in favourites yet.</div>";
    }
?>

我使用add_to_fav.php以下内容将产品添加到我的收藏夹:

<?php
    session_start();

    // get the product id
    $prod_id = $_GET['prod_id'];
    $prod_name = $_GET['prod_name'];

    /* 
     * check if the 'fav' session array was created
     * if it is NOT, create the 'fav' session array
     */

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

    // check if the item is in the array, if it is, do not add
    if (in_array($prod_id, $_SESSION['fav'])) {
        // redirect to product list and tell the user it was added to favourites
        header('Location: prod_list.php?action=exists&prod_id' . $prod_id . '&prod_name=' .    $prod_name);
    }

    // else, add the item to the array
    else {
        array_push($_SESSION['fav'], $prod_id);
        // redirect to product list and tell the user it was added to cart
        header('Location: prod_list.php?action=add&prod_id' . $prod_id . '&prod_name=' .  $prod_name);
    }

 ?>

当我尝试查看收藏夹时,我“没有在您的收藏夹中找到产品。:(”

我有一个类似计数器的东西,它也显示了我最喜欢的产品数量,它保持为 0。

我在哪里做错了吗?我应该纠正哪个错误?

4

1 回答 1

0

有几件事情可能会发生。

1)您没有在加载收藏夹之前开始会话:

<div class='sectionContents'>
<?php
if(isset($_GET['action']) && $_GET['action']=='removed'){
    echo "<div>" . $_GET['prod_name'] . " was removed from favourites.</div>";
}

session_start()

if(isset($_SESSION['fav'])){

2)您的 SQL 查询实际上没有找到任何产品 ID。您可能想调试 SQL 并在 phpmyadmin 或您的 mysql 界面中运行它,以查看它是否确实返回任何结果。

include "db_connect.php";

$query = "SELECT prod_id, prod_name, prod_price FROM tbl_product WHERE prod_id IN ('$ids')";
echo $query; // Print query for debugging
$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);

我的猜测是这个查询是不正确的,因为周围有单引号$ids

它应该是:

$query = "SELECT prod_id, prod_name, prod_price FROM tbl_product WHERE prod_id IN ($ids)";

这也可以简化为:

$ids = "";
foreach($_SESSION['fav'] as $prod_id){
    $ids = $ids . $prod_id . ",";
}

// remove the last comma
$ids = rtrim($ids, ',');

至:

$ids = implode(",", $_SESSION['fav']);
于 2013-06-21T16:56:55.150 回答