php - 在 php 基本库存项目中显示产品缺货
问题描述
嘿,我正在尝试制作一个基本的库存项目,我想在销售产品按钮上显示缺货的消息。如果 balance_quantity 为 0,则基本上在数据库中,则条目应停止并显示一条消息。尝试定义变量 $balance_quantity = "0"; 并替换 if 和 elseif 这是我的代码。
<?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)
}
推荐阅读
- wordpress - HTTPS 图片请求在 Wordpress 中变成 HTTP 请求
- asp.net - 如何在每次单击按钮时不将迭代器重置为 0 来遍历数组?
- excel - 如何使用vba将单列从word复制到预先存在的excel表?
- regex - 301 重定向 www.domain 到 domain.com
- vscode-settings - VSCODE:我如何默认启用“任务:允许文件夹中的自动任务”?
- swift - 迅速减少双倍
- php - 从特定表中提取链接
- postgresql - PostgreSql 短语to_tsquery 很慢
- python - 如何在测试执行期间找到参数化测试的测试 ID?
- angular - 使用缓存在自定义 API 服务中模拟 HttpClient