Php 迷你购物车有问题吗

Php 迷你购物车有问题吗,php,mysql,sql,printf,Php,Mysql,Sql,Printf,我正在做一个迷你购物车。一切似乎都正常,但是,当我点击不同的产品时,它无法提取正确的数据,例如 我选择产品1-它为我提供了正确的信息,但如果我选择产品2,它将为我提供与产品1相同的信息 我将在下面显示我的代码,但我相信错误来自这行代码: $sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode); 我不认为它能得到$bikecode 这是php文件中的全部代码: <?php $b

我正在做一个迷你购物车。一切似乎都正常,但是,当我点击不同的产品时,它无法提取正确的数据,例如

我选择产品1-它为我提供了正确的信息,但如果我选择产品2,它将为我提供与产品1相同的信息

我将在下面显示我的代码,但我相信错误来自这行代码:

$sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode); 
我不认为它能得到$bikecode

这是php文件中的全部代码:

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

看起来您得到了它,但为了方便其他可能搜索它的人参考,问题是您在sprintf()中使用了%d,试图在%d被视为整数时表示varchar。使用%s将解决此问题


在将$bikecode包含在查询中之前,您是否尝试过将其显式转换为
int
?ie:
$sql=sprintf(“选择型号,从Bike中选择价格,其中BikeCode=%d;”,intval($BikeCode))
除此之外,
将查询回送到页面并检查它是否按您期望的方式形成。my$bikecode不是整数,它是一个varchar。my bike code包含文本和数字。那么为什么在
sprintf()
中使用
%d
标志,为什么不在查询中用引号括起来?使用这个:
sprintf(“从Bike中选择型号,价格,其中BikeCode='%s';”,$BikeCode)成功了!我对sprintf了解不多,只是在谷歌上搜索了一下,你是对的%d表示整数,%s表示字符串。非常感谢你。
$sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode);