以下是一个简单的PHP代码示例,用于生成库存调拨功能:
<?php
// 假设有两个表:inventory(库存表)和transfer(调拨表)
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "inventory_management";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 处理库存调拨请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$from_location = $_POST["from_location"];
$to_location = $_POST["to_location"];
$product_id = $_POST["product_id"];
$quantity = $_POST["quantity"];
// 检查库存是否足够
$check_inventory_sql = "SELECT quantity FROM inventory WHERE location = '$from_location' AND product_id = '$product_id'";
$check_inventory_result = $conn->query($check_inventory_sql);
if ($check_inventory_result->num_rows > 0) {
$row = $check_inventory_result->fetch_assoc();
$current_quantity = $row["quantity"];
if ($current_quantity >= $quantity) {
// 更新库存
$update_inventory_sql = "UPDATE inventory SET quantity = quantity - $quantity WHERE location = '$from_location' AND product_id = '$product_id'";
$conn->query($update_inventory_sql);
// 插入调拨记录
$insert_transfer_sql = "INSERT INTO transfer (from_location, to_location, product_id, quantity) VALUES ('$from_location', '$to_location', '$product_id', '$quantity')";
$conn->query($insert_transfer_sql);
echo "库存调拨成功!";
} else {
echo "库存不足,无法完成调拨!";
}
} else {
echo "库存不存在,无法完成调拨!";
}
}
?>
<!-- HTML 表单 -->
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<label for="from_location">调出库位:</label>
<input type="text" name="from_location" id="from_location" required><br>
<label for="to_location">调入库位:</label>
<input type="text" name="to_location" id="to_location" required><br>
<label for="product_id">产品ID:</label>
<input type="text" name="product_id" id="product_id" required><br>
<label for="quantity">调拨数量:</label>
<input type="number" name="quantity" id="quantity" required><br>
<input type="submit" value="提交">
</form>
以上代码示例中,假设数据库中有两个表:inventory(库存表)和transfer(调拨表)。代码首先连接到数据库,然后处理库存调拨请求。在表单中,用户需要输入调出库位、调入库位、产品ID和调拨数量。代码会先检查库存是否足够,如果足够则更新库存并插入调拨记录,否则返回相应的错误信息。
请根据实际需求和数据库结构进行适当的修改。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站