设置最大输入数量

时间:2017-08-20 02:43:07

标签: php

我的问题非常复杂。我目前正在研究贷款制度。我有两个关于贷款模块的表格:

  • 项目
  • 贷款

当物品被借出时,借出的数量将从库存中减去(item_qty - loan_qty)。我的问题来自更新贷款模块。我不知道如何设置最大数量。我知道我只需要添加item_qty和loan_qty,但我找不到这样做的方法。

我用谷歌搜索了很长时间但仍然卡住了。这是我的代码:

<?php
    $loan_id=$_GET['loan_id'];
    $acc_query = $con->query("SELECT * FROM `loan` natural join `item` WHERE loan_id = '$_REQUEST[loan_id]'  ") or die(mysqli_error());
    $acc_fetch = $acc_query->fetch_array();
?>

<div>
<form method="post" action="update_loan.php" >
    <input type="hidden" name="item_id" value="<?php echo $item_id; ?>">
    <input type="hidden" name="loan_id" value="<?php echo $loan_id; ?>">  

    <div class = "form-group">
        <label>Quantity</label>
        <input name="n_qty" type = "number" id = "n_qty" value= "<?php echo $acc_fetch['qty']?>" class = "form-control" min="1" max="??" />
    </div>
</div>

表库存:

  • ITEM_ID
  • ITEM_CODE
  • ITEM_NAME
  • item_qty

表贷款:

  • loan_id
  • ITEM_ID
  • loan_qty
  • loaned_place

2 个答案:

答案 0 :(得分:0)

您可以简单地按item_id, item_code, item_name使用分组,然后使用sum()函数从两个表中获取qty的总和:inventoryloan,并减去以获得所需的数量之一。

SELECT i.item_id,i.item_code,i.item_name, (SUM(i.item_qty) - SUM(l.loan_qty)) AS ResultQty
FROM `inventory` i
natural join `loan` l
GROUP BY i.item_id,i.item_code,i.item_name

答案 1 :(得分:0)

感谢您帮助我们。我有一些想法。这是我的解决方案:

<?php
$loan_id=$_GET['loan_id'];
$acc_query = $con->query("SELECT * FROM `loan` natural join `item` WHERE loan_id = '$_REQUEST[loan_id]'  ") or die(mysqli_error());
$acc_fetch = $acc_query->fetch_array();

$qty1 = $acc_fetch['loan_qty']; // declaring the Loan quantity as $qty1
$item_id = $acc_fetch["item_id"]; // get the item_id from the loan table

$sql = $con->query("SELECT * FROM `item` WHERE item_id = '$item_id'  ") or die(mysqli_error());
$row = $sql->fetch_array();

$qty2 = $row['item_qty']; // declaring the item quantity from as $qty2

$qtyy = $qty1 + $qty2; // sum up the quantity 

    <div>
    <form method="post" action="update_loan.php" >
    <input type="hidden" name="item_id" value="<?php echo $item_id; ?>">
    <input type="hidden" name="loan_id" value="<?php echo $loan_id; ?>">  

    <div class = "form-group">
    <label>Quantity</label>
    <input name="n_qty" type = "number" id = "n_qty" value= "<?php echo 
    $acc_fetch['qty']?>" class = "form-control" min="1" max="<?php echo "$qtyy"; ?>" /> // set the maximum number input  
    </div>
    </div>
   ?>