添加到收藏夹的代码无提示失败

时间:2013-06-21 16:37:42

标签: php mysql

我有一个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。

我在某处错了吗?我应该纠正哪个错误?

1 个答案:

答案 0 :(得分: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']);