Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

对PHP写库存管理系统中的商品退货功能进行代码生成

作者:昔日暖阳   发布日期:2023-11-09   浏览:908

以下是一个简单的PHP代码示例,用于生成库存管理系统中的商品退货功能:

<?php
// 连接数据库
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 处理退货请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $product_id = $_POST["product_id"];
    $quantity = $_POST["quantity"];
    $reason = $_POST["reason"];

    // 检查库存是否足够
    $sql = "SELECT quantity FROM products WHERE id = $product_id";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $current_quantity = $row["quantity"];
        if ($current_quantity < $quantity) {
            echo "库存不足,请重新输入退货数量。";
            exit;
        }
    } else {
        echo "商品不存在。";
        exit;
    }

    // 更新库存数量
    $new_quantity = $current_quantity - $quantity;
    $sql = "UPDATE products SET quantity = $new_quantity WHERE id = $product_id";
    if ($conn->query($sql) === TRUE) {
        // 保存退货记录
        $sql = "INSERT INTO returns (product_id, quantity, reason) VALUES ($product_id, $quantity, '$reason')";
        if ($conn->query($sql) === TRUE) {
            echo "退货成功。";
        } else {
            echo "退货记录保存失败。";
        }
    } else {
        echo "更新库存失败。";
    }
}

$conn->close();
?>

<!-- HTML表单 -->
<!DOCTYPE html>
<html>
<body>

<h2>商品退货</h2>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
  <label for="product_id">商品ID:</label>
  <input type="text" id="product_id" name="product_id"><br><br>
  <label for="quantity">退货数量:</label>
  <input type="text" id="quantity" name="quantity"><br><br>
  <label for="reason">退货原因:</label>
  <input type="text" id="reason" name="reason"><br><br>
  <input type="submit" value="提交">
</form>

</body>
</html>

请注意,这只是一个简单的示例代码,你可能需要根据实际需求进行修改和完善。另外,为了安全起见,你应该对用户输入进行验证和过滤,以防止SQL注入等安全问题。

上一篇:PHP表单处理:复选框与多选框数据处理

下一篇:PHP和XML:如何实现用户登录和权限管理

大家都在看

php session用法

phpisset函数

php后端

php爬虫框架

php读取csv文件

php 三元表达式

php文件加密

php 拆分字符串

php pcntl

php ||

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站