Home > Backend Development > PHP Tutorial > PHP Mall Function Tutorial: Creating Inventory and Inventory Management System

PHP Mall Function Tutorial: Creating Inventory and Inventory Management System

WBOY
Release: 2023-07-29 08:52:01
Original
1590 people have browsed it

PHP Mall Function Tutorial: Creating Inventory and Inventory Management System

Introduction:
In an online mall, inventory management is a very important function. Accurately tracking and managing inventory can help avoid out-of-stock and over-stock issues and improve sales efficiency and customer satisfaction. This tutorial will teach you how to use PHP to create a simple but powerful inventory management system to effectively manage product inventory.

1. Database design
First, we need to create a database to store information related to products and inventory. We create a database named "inventory_management" and create two tables in it: "products" and "stock". The following is the structural design of the tables.

Table: products
Field:

  • id: integer, primary key, auto-increment
  • name: string, product name
  • price: floating point type, product price
  • description: string, product description

Table: stock
Field:

  • id: Integer type, primary key, auto-increment
  • product_id: integer type, foreign key, associated products table id field
  • quantity: integer type, product inventory quantity

二, Create page
Create a folder named "inventory" in the website directory, and create three files in it: "index.php", "add_product.php" and "manage_stock.php".

  1. index.php
    This file will be the home page of our inventory management system. It will display all product and inventory information.

Code example:

<?php
// 连接数据库 
$conn = mysqli_connect("localhost", "root", "", "inventory_management");

// 查询所有商品和库存信息
$query = "SELECT p.name, p.price, p.description, s.quantity FROM products p INNER JOIN stock s ON p.id = s.product_id";
$result = mysqli_query($conn, $query);

// 显示商品和库存信息
echo "<h1>库存管理系统</h1>";
echo "<table>";
echo "<tr><th>商品名称</th><th>价格</th><th>描述</th><th>库存数量</th></tr>";
while($row = mysqli_fetch_assoc($result)) {
    echo "<tr><td>".$row['name']."</td><td>".$row['price']."</td><td>".$row['description']."</td><td>".$row['quantity']."</td></tr>";
}
echo "</table>";

// 关闭数据库连接
mysqli_close($conn);
?>
Copy after login
  1. add_product.php
    This file will allow users to add new products to the mall.

Code example:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    // 获取用户输入的商品信息
    $name = $_POST["name"];
    $price = $_POST["price"];
    $description = $_POST["description"];

    // 连接数据库
    $conn = mysqli_connect("localhost", "root", "", "inventory_management");

    // 添加商品到products表
    $query = "INSERT INTO products (name, price, description) VALUES ('$name', '$price', '$description')";
    mysqli_query($conn, $query);

    // 获取新增商品的id
    $product_id = mysqli_insert_id($conn);

    // 添加库存信息到stock表
    $quantity = $_POST["quantity"];
    $query = "INSERT INTO stock (product_id, quantity) VALUES ($product_id, $quantity)";
    mysqli_query($conn, $query);

    // 提示用户商品添加成功
    echo "商品已成功添加!";

    // 关闭数据库连接
    mysqli_close($conn);
}
?>

<form method="post" action="">
    <label for="name">商品名称:</label>
    <input type="text" name="name" required><br><br>
    <label for="price">价格:</label>
    <input type="number" name="price" min="0" step="0.01" required><br><br>
    <label for="description">描述:</label><br>
    <textarea name="description" rows="4" cols="50" required></textarea><br><br>
    <label for="quantity">库存数量:</label>
    <input type="number" name="quantity" min="0" required><br><br>
    <input type="submit" value="添加商品">
</form>
Copy after login
  1. manage_stock.php
    This file allows the administrator to modify the inventory quantity of the product.

Code sample:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    // 获取用户输入的商品id和库存数量
    $product_id = $_POST["product_id"];
    $quantity = $_POST["quantity"];

    // 连接数据库
    $conn = mysqli_connect("localhost", "root", "", "inventory_management");

    // 更新stock表中的库存数量
    $query = "UPDATE stock SET quantity=$quantity WHERE product_id=$product_id";
    mysqli_query($conn, $query);

    // 提示用户库存数量已更新
    echo "库存数量已成功更新!";

    // 关闭数据库连接
    mysqli_close($conn);
}
?>

<form method="post" action="">
    <label for="product_id">请选择商品:</label>
    <select name="product_id">
        <?php
        // 连接数据库
        $conn = mysqli_connect("localhost", "root", "", "inventory_management");

        // 查询所有商品
        $query = "SELECT * FROM products";
        $result = mysqli_query($conn, $query);

        // 显示商品选项
        while($row = mysqli_fetch_assoc($result)) {
            echo "<option value=".$row['id'].">".$row['name']."</option>";
        }

        // 关闭数据库连接
        mysqli_close($conn);
        ?>
    </select><br><br>
    <label for="quantity">库存数量:</label>
    <input type="number" name="quantity" min="0" required><br><br>
    <input type="submit" value="更新库存数量">
</form>
Copy after login

Conclusion:
Through this tutorial, you learned how to use PHP to create a simple inventory management system. You can modify and extend the above sample code as needed to meet the needs of actual projects. I hope this tutorial can help you better manage your mall's inventory and improve sales efficiency and user satisfaction.

The above is the detailed content of PHP Mall Function Tutorial: Creating Inventory and Inventory Management System. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template