dtpk04526211 2013-06-21 16:37
浏览 12

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

I have a myList.php which should list all products added to my favourites and compute the total price of products.

here is the code:

<?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>";
    }
?>

I use the add_to_fav.php below to add the products to my favourites:

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

 ?>

I am having "No products found in your favourites. :(" when i try to view the favourites

I have a counter like thing which shows the number of products in my favourites as well and it stays to 0.

Have I erred somewhere? Which mistake should I correct?

  • 写回答

1条回答 默认 最新

  • dou31797719 2013-06-21 16:56
    关注

    There are a few things that could be happening.

    1) You are not starting the session before loading the favorites:

    <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) Your SQL query in fact is not finding any product ids. You might want to debug the SQL and run it in phpmyadmin or your mysql interface to see if it in fact does return any results.

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

    My guess is that this query is incorrect because of the single quotes around $ids

    It should be:

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

    Also this can be simplified from:

    $ids = "";
    foreach($_SESSION['fav'] as $prod_id){
        $ids = $ids . $prod_id . ",";
    }
    
    // remove the last comma
    $ids = rtrim($ids, ',');
    

    To:

    $ids = implode(",", $_SESSION['fav']);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据