Home  >  Article  >  Backend Development  >  How to use PHP to write product shipping function code in the inventory management system

How to use PHP to write product shipping function code in the inventory management system

PHPz
PHPzOriginal
2023-08-06 17:49:441583browse

How to use PHP to write the product outbound function code in the inventory management system

The inventory management system is a very important link in enterprise management, and one of its core functions is the outbound management of goods. In the inventory management system, the outbound function code of goods must be able to reduce the quantity of goods and generate sales records. This article will introduce how to use PHP to write product shipping function code in the inventory management system, and provide corresponding code examples.

1. Database design
Before we start writing code, we need to design the database first. First, we create a database called inventory. In this database, we create two tables: products and sales. The

products table contains the following fields:

  • id (int): product ID
  • name (varchar): product name
  • quantity ( int): Product quantity

The sales table contains the following fields:

  • id (int): Sales record ID
  • product_id (int): Product ID
  • quantity (int): Sales quantity
  • date (date): Sales date

2. Write the product delivery function code

  1. Connecting to the database
    First, we need to write PHP code to connect to the database. The following is a code example for connecting to the database:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "inventory";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接数据库失败: " . $conn->connect_error);
}
  1. Get product information
    Next, we need to write code to obtain product information. The following is a code example for obtaining product information:
<?php
$sql = "SELECT id, name, quantity FROM products";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "商品ID: " . $row["id"]. " - 商品名称: " . $row["name"]. " - 商品数量: " . $row["quantity"]. "<br>";
    }
} else {
    echo "没有商品";
}
  1. Product delivery
    Next, we need to write code to implement the function of product delivery. The following is a code example for product shipment:
<?php
$product_id = $_POST['product_id'];
$quantity = $_POST['quantity'];

// 检查商品数量是否足够
$sql = "SELECT quantity FROM products WHERE id = $product_id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($quantity > $row["quantity"]) {
    echo "商品数量不足";
    exit;
}

// 更新商品数量
$new_quantity = $row["quantity"] - $quantity;
$sql = "UPDATE products SET quantity = $new_quantity WHERE id = $product_id";
$conn->query($sql);

// 生成销售记录
$sql = "INSERT INTO sales (product_id, quantity, date) VALUES ($product_id, $quantity, NOW())";
$conn->query($sql);

echo "商品出库成功";

The above code first obtains the product ID and shipment quantity submitted by the user. It then checks if the item quantity is sufficient and prompts the user if not. If the quantity of goods is sufficient, the quantity of goods is updated and the corresponding quantity is reduced. Next, the code generates a sales record and inserts the product ID, outbound quantity and current date into the sales table. Finally, a prompt indicating that the library was successfully exported is displayed.

3. Summary
This article introduces how to use PHP to write the product outbound function code in the inventory management system, and provides corresponding code examples. Through the above code, we can reduce the quantity of goods and generate sales records, adding important functions to the inventory management system. However, it should be noted that this is only a code example for the outbound function. The actual inventory management system also needs to consider other functions and security, and carry out corresponding development and testing.

The above is the detailed content of How to use PHP to write product shipping function code in the inventory management system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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