我试图阻止用户购买缺货产品。以下代码未显示任何错误,并且即使库存低于零,仍允许用户购买产品。关于我做错了什么的任何建议?
<?php
$productID= $_GET['ID'];
$username = $_SESSION['solentuser'];
$conn=new PDO("mysql:host=localhost;dbname=user;","user","pass");
$results=$conn->query("SELECT stocklevel FROM products");
$row=$results->fetch();
if($row['stocklevel'] >= 1){
$conn->query("INSERT INTO basket(productID,username,qty) values($productID,'$username',1)");
$conn->query("UPDATE products SET stocklevel=stocklevel-1 WHERE ID=$productID");
echo "You have successfully added this product to your basket!";
echo "<br><br><p> <a href='basket.php'>View my basket!</a></p>";
}
else{
echo "This product is out of stock!";
echo "<p><a href='index.php'>Back to Search page</a></p>";
}
//print_r($conn->errorInfo());
?>
答案 0 :(得分:1)
可能需要更改为:SELECT stocklevel FROM products WHERE id=$productID
因为之前您从所有产品中选择了库存水平
答案 1 :(得分:1)
您必须按产品ID
选择产品<?php
$productID= $_GET['ID'];
$username = $_SESSION['solentuser'];
$conn=new PDO("mysql:host=localhost;dbname=user;","user","pass");
$results=$conn->query("SELECT stocklevel FROM products Where productID=$productID");
$row=$results->fetch();
if($row['stocklevel'] >= 1){
$conn->query("INSERT INTO basket(productID,username,qty) values($productID,'$username',1)");
$conn->query("UPDATE products SET stocklevel=stocklevel-1 WHERE ID=$productID");
echo "You have successfully added this product to your basket!";
echo "<br><br><p> <a href='basket.php'>View my basket!</a></p>";
}
else{
echo "This product is out of stock!";
echo "<p><a href='index.php'>Back to Search page</a></p>";
}
//print_r($conn->errorInfo());