Вопрос

I am having problems creating a basket page. Everytime i add a product it shows me that a product has been added but does not display the product information...

This is the code on my 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;
    }
?>

I believe the error comes from this section in the code, but i can't find out what it is:

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

Help would be appreciated and thank you in advance.

EDIT

This is what the error is apparently:

Notice: Use of undefined constant BikeCode - assumed 'BikeCode' in /homes/2012/abpr904/html/velocity/basket.php on line 42 Notice: Undefined index: BikeCode in /homes/2012/abpr904/html/velocity/basket.php on line 42 Notice: Use of undefined constant action - assumed 'action' in /homes/2012/abpr904/html/velocity/basket.php on line 43 Notice: Undefined index: action in /homes/2012/abpr904/html/velocity/basket.php on line 43 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /homes/2012/abpr904/html/velocity/basket.php on line 76 Notice: Undefined variable: total in /homes/2012/abpr904/html/velocity/basket.php on line 94

Это было полезно?

Решение

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

You're using sprintf incorrectly.

But you should switch to PDO and sanitize properly.

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top