在php基本库存项目中显示产品缺货
嘿,我正在尝试做一个基本的库存项目,我想在其中显示的消息,脱销产品按钮的库存。基本上在数据库中,如果余额数量为0,则条目应停止并显示一条消息。尝试定义变量$balance\u quantity=“0”;而且 替换if和elseif 这是我的密码在php基本库存项目中显示产品缺货,php,inventory-management,Php,Inventory Management,嘿,我正在尝试做一个基本的库存项目,我想在其中显示的消息,脱销产品按钮的库存。基本上在数据库中,如果余额数量为0,则条目应停止并显示一条消息。尝试定义变量$balance\u quantity=“0”;而且 替换if和elseif 这是我的密码 <?php $db = mysqli_connect('localhost', 'root', '', 'inventory'); if (isset($_GET['sell'])) { $item_id =
<?php
$db = mysqli_connect('localhost', 'root', '', 'inventory');
if (isset($_GET['sell'])) {
$item_id = $_GET['sell'];
$balance_quantity = $_GET['bal']-1;
$record = mysqli_query($db, "UPDATE item_master SET balance_quantity=$balance_quantity WHERE item_id=$item_id");
}
if(isset($_GET['buy'])) {
$item_id = $_GET['buy'];
$quantity = 1;
$query = "INSERT INTO transaction (Item_id, quantity) VALUES ($item_id,$quantity)" ;
mysqli_query($db, $query);
}
?>
<td>
<a href="cart.php?sell=<?php echo $row['item_id']; ?>&bal=<?php echo $row['balance_quantity']; ?>" class="btn">Sell</a>
</td>
<td>
<a href="cart.php?buy=<?php echo $row['item_id']; ?>" class="btn">Buy</a>
</td>
只是一个建议:从数据库中获取当前余额并进行比较 在这里,您将从一个GET值获得余额。如果另一个用户同时购买或销售该怎么办 例如:
$db = mysqli_connect('localhost', 'root', '', 'inventory');
if (isset($_GET['sell'])) {
$item_id = $_GET['sell'];
$balance_query= mysqli_fetch_assoc(mysqli_query($db, "SELECT balance_quantity FROM item_master WHERE item_id=$item_id"));
$balance_quantity = balance_query['balance_quantity'];
//Rest of logic. Or even better, make a function that plain returns getBalance($itemId)
}
只需选择“余额数量”并检查其是否大于0。此外,您不应该使用这样的查询,因为它容易受到SQL注入的攻击。请看一看。sure将记住这一点,并使用>0进行了检查,但给出了相同的输出($msg),即使balance_quantity表的值大于0。