首页 > 解决方案 > 在 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>

标签: phpinventory-management

解决方案


只是一个建议:做一些从数据库中获取当前余额的东西并比较它们。

在这里,您从 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)

    }

推荐阅读