0

我在创建购物篮页面时遇到问题。每次我添加产品时,它都会向我显示已添加产品但不显示产品信息...

这是我的 basket.php 上的代码:

<?php

    $id = $_GET[BikeCode];   //the product id from the URL 
    $action     = $_GET[action]; //the action from the URL 

    //if there is an id and that id doesn't exist display an error message
    if($id && !productExists($id)) {
        die("Error. Product Doesn't Exist");
    }

    switch($action) {   //decide what to do 

        case "add":
            $_SESSION['cart'][$id]++; //add one to the quantity of the product with id $id 
        break;

        case "remove":
            $_SESSION['cart'][$id]--; //remove one from the quantity of the product with id $id 
            if($_SESSION['cart'][$id] == 0) unset($_SESSION['cart'][$id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
        break;

        case "empty":
            unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
        break;
    }


    if($_SESSION['cart']) {

        echo "<table width=\"100%\">";   

            foreach($_SESSION['cart'] as $id => $quantity) {    

              $sql = sprintf("SELECT Manufacturer, Model, Price FROM Bike WHERE BikeCode = $id;", $id); 


                $result = mysql_query($sql);

                if(mysql_num_rows($result) > 0) {

                    list($Manufacturer, $Model, $Price) = mysql_fetch_row($result);

                    $line_cost = $price * $quantity;        //work out the line cost
                    $total = $total + $line_cost;           //add to the total cost

                        echo "<tr><th>Manufacturer:</th><th>Model:</th><th>Price:</th></tr>";
                        echo "<tr>";
                        echo "<td align=\"center\">$Manufacturer</td>";
                        echo "<td align=\"center\">$Model</td>";
                        echo "<td align=\"center\">$Price</td>";
                    echo "</tr>";
                }
            }

            echo "<tr>";
                echo "<td colspan=\"2\" align=\"right\">Total</td>";
                echo "<td align=\"right\">£$total</td>";
            echo "</tr>";

            echo "<tr>";
                echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
            echo "</tr>";       
        echo "</table>";

    }else{
        echo "You have no items in your shopping cart.";
    }

    function productExists($id) {
            $sql = "SELECT * FROM Bike WHERE BikeCode = $id";

            return mysql_num_rows(mysql_query($sql)) > 0;
    }
?>

我相信错误来自代码中的这一部分,但我无法找出它是什么:

foreach($_SESSION['cart'] as $id => $quantity) {    

                  $sql = sprintf("SELECT Manufacturer, Model, Price FROM Bike WHERE BikeCode = $id;", $id); 


                    $result = mysql_query($sql);

                    if(mysql_num_rows($result) > 0) {

                        list($Manufacturer, $Model, $Price) = mysql_fetch_row($result);

                        $line_cost = $price * $quantity;        //work out the line cost
                        $total = $total + $line_cost;           //add to the total cost

                            echo "<tr><th>Manufacturer:</th><th>Model:</th><th>Price:</th></tr>";
                            echo "<tr>";
                            echo "<td align=\"center\">$Manufacturer</td>";
                            echo "<td align=\"center\">$Model</td>";
                            echo "<td align=\"center\">$Price</td>";
                        echo "</tr>";
                    }
                }

帮助将不胜感激,并提前感谢您。

编辑

这显然是错误:

注意:使用未定义的常量 BikeCode - 在第 42 行的 /homes/2012/abpr904/html/velocity/basket.php 中假定为“BikeCode” 注意:未定义的索引:/homes/2012/abpr904/html/velocity/basket 中的 BikeCode。第 42 行的 php 注意:使用未定义的常量动作 - 在第 43 行的 /homes/2012/abpr904/html/velocity/basket.php 中假定为“动作” 注意:未定义的索引:/homes/2012/abpr904/html/ 中的动作第 43 行的 velocity/basket.php 警告:mysql_num_rows() 期望参数 1 是资源,布尔值在第 76 行的 /homes/2012/abpr904/html/velocity/basket.php 中给出 注意:未定义的变量:/homes/ 中的总数2012/abpr904/html/velocity/basket.php 第 94 行

4

1 回答 1

0
$sql = sprintf("SELECT Manufacturer, Model, Price FROM Bike WHERE BikeCode = %d;", $id); 

您使用 sprintf 不正确。

但是您应该切换到PDO并正确消毒。

Also, I assume you mean $id = $_GET['id'] or some constant identifier. It would be insane to assign a unique get param for each item.

于 2012-11-24T01:17:39.450 回答