我在创建一个篮子页面时遇到了问题。我每次添加产品时都会向我显示已添加产品但未显示产品信息......
这是我的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中假定为'action'注意:未定义的索引:/ homes / 2012 / abpr904中的操作第43行/html/velocity/basket.php 警告:mysql_num_rows()期望参数1为资源,第76行/homes/2012/abpr904/html/velocity/basket.php中给出布尔值。注意:未定义变量:/ homes / 2012 / abpr904 / html / velocity /中的总数第94行的basket.php
答案 0 :(得分:0)
$sql = sprintf("SELECT Manufacturer, Model, Price FROM Bike WHERE BikeCode = %d;", $id);
您正在错误地使用sprintf。
但你应该切换到PDO并正确消毒。
另外,我假设你的意思是$ id = $ _GET ['id']或一些常量标识符。为每个项目分配一个唯一的get参数将是疯狂的。