dongxingqiu7943 2012-12-07 16:50
浏览 23
已采纳

迷你购物车有问题

i am working on a mini shopping cart. Everything seems to work, however, it does not extract the right data when i click on a different product for e.g.

I choose Product 1 - It gives me the right information but if i choose Product 2 it will give me the same information as product 1.

I will display my code below, but i believe the error is coming from this line of code:

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

I don't think its getting the $bikecode

This is the whole code within the php file:

<?php
    $bikecode = $_GET['id'];     //the product id from the URL 
    $action = $_GET['action']; //the action from the URL

if($bikecode && !productExists($bikecode)) {
    die("Product Doesn't Exist");
} 

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

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

        case "remove":
            $_SESSION['cart'][$bikecode]--; //remove one from the quantity of the product with id $bikecode 
            if($_SESSION['cart'][$bikecode] == 0) unset($_SESSION['cart'][$bikecode]); //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 $bikecode => $quantity) { 

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

                $result = mysqli_query($con, $sql);

                if(mysqli_num_rows($result) > 0) {

                    list($model, $price) = mysqli_fetch_row($result);   

                    $cost = $quantity * $price;
                    $total = $total + $cost;

                        echo "<tr><th>Model:</th><th>Quantity:</th><th>Price:</th></tr>";
                        echo "<tr>";
                        echo "<td align=\"center\">$model</td>";
                        echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$bikecode\">X</a></td>";
                        echo "<td align=\"center\">£$cost</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($bikecode) {
    $sql = sprintf("SELECT * FROM Bike WHERE BikeCode = %d;", $bikecode); 
    return mysqli_num_rows(mysqli_query($con, $sql)) > 0;
}
?>

Help would be extremely appreciated but if you cannot find an error could you please direct me in another way of displaying this code:

$sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode);
  • 写回答

1条回答 默认 最新

  • doukaojie8573 2012-12-07 17:21
    关注

    It looks like you got it but for easy reference to others that might search it the problem is that you were using %d in sprintf() to try and represent a varchar when %d is treated as an integer. Using %s will solve the problem.

    sprintf() - Manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!