【问题标题】:Receiving Errors in PHP在 PHP 中接收错误
【发布时间】:2012-11-24 01:13:11
【问题描述】:

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

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

【问题讨论】:

    标签: mysql cart php shopping


    【解决方案1】:
    $sql = sprintf("SELECT Manufacturer, Model, Price FROM Bike WHERE BikeCode = %d;", $id); 
    

    您错误地使用了 sprintf。

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

    另外,我假设您的意思是 $id = $_GET['id'] 或一些常量标识符。为每个项目分配一个唯一的 get 参数是很疯狂的。

    【讨论】:

    • 如何修复错误或切换到 PDO。我还是个初学者,非常感谢您的帮助,BikeCode 是我表中的主键,id 只是一个变量。
    • 我更新了您的查询。只需将 $id 更改为 %d ,就像它显示的那样。此外,您可以使用以下命令检查 mysql 错误: $result = mysql_query($sql) 或 die(mysql_error); ... 至于切换到 PDO。您必须单击链接才能开始。按照教程。习惯并不难,但如果你是初学者,你肯定需要这样做,忘记曾经存在过的 mysql_ 函数。不开玩笑。这是必须的。
    • 非常感谢您的建议。顺便说一句,它出现了 mysql_error 你认为我需要重新启动篮子来解决这个问题还是可以解决?
    • 所有问题都可以解决。不过,如果您发布实际错误会很有帮助。
    • 就像我在回答中所说, $id = $_GET['id'] 是您应该使用的。您需要以通用方式传递项目代码。这将解决该错误。你正在使用一个常量,谁知道你可能在哪里定义它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多