我当前正在尝试在我下订单的地方形成逻辑,如果订单与当前库存的总和超过了容量,则会出现相应的消息。另外,如果有进货订单,则在添加新订单时,它们不能超过与待处理进货订单以及当前可用库存相加的容量。但是,我的问题是,无论我投入多少,它都会持续显示“下订单的库存量已超出容量”。验证消息。我对此功能的逻辑有误吗?谢谢。
if(isset($_POST['addBtn'])){
$brand = $_POST['brand'];
$modelName = $_POST['model_name'];
$session = $_SESSION['username'];
$station = 'At Factory';
$progress = 'Awaiting';
$status = 'Awaiting';
$handler = 'Awaiting';
$request = '-';
$stock = $_POST['stock'];
$incoming = '0';
$date = date('Y-m-j H:i:s', time());
$stock = $_POST['stock'];
$incoming = '0';
$sql2 = "SELECT o.stock,m.current,m.capacity FROM orders o
INNER JOIN models m ON o.model_id=m.model_id
WHERE o.incoming = 0";
$result2 = $conn->query($sql2);
if(!$result2) die($conn->error);
$rows = $result2->num_rows;
$stok = $rows['stock'];
$cu = $rows['current'];
$ca = $rows['capacity'];
if(($stock+$cu)>$ca){
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong id="alert2">Error! </strong>Placed order stock amount has exceeded capacity.
</div>
<?php
}elseif(($stok+$stock+$cu)>$ca){
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong id="alert2">Error! </strong>Pending incoming stocks has exceeded capacity.
</div>
<?php
}else{
$query = "INSERT INTO orders(brand,model_id,station,progress,status,editor,time_ordered,admin,request) VALUES ('".$brand."','".$modelName."','".$station."','".$progress."','".$status."','".$handler."','".$date."','".$session."','".$request."')";
}
}
答案 0 :(得分:2)
我相信您将需要在SQL中使用另一个WHERE子句来根据模型选择订单。
示例:
$sql2 = "SELECT SUM(o.stock) as stock_sum, m.current, m.capacity FROM orders o
INNER JOIN models m ON o.model_id=m.model_id
WHERE o.incoming = 0 AND m.model_id = $modelName";
但是,如果没有订单,则可能会发生错误。例如$ cu和$ ca可能为空,因为您通过加入订单和模型来获得这些值。
最好的建议是使用单独的SQL来检索库存总额,并使用sql来检索模型详细信息。