How to use PHP to write the basic framework code of the inventory management system
In the modern business environment, inventory management is crucial to the operation of the enterprise. In order to manage inventory more efficiently, many companies have adopted automated inventory management systems. This article will introduce how to use PHP to write the basic framework code of the inventory management system, and provide some code examples to help readers better understand.
First, we need to design a suitable database to store inventory information. We can create a database named "inventory_management" and create the following table in it:
a. products: used to store product information, including product ID (product_id), product name (product_name) and product inventory (product_stock) and other fields.
b. transactions: used to store transaction information, including fields such as transaction ID (transaction_id), transaction type (transaction_type), and transaction quantity (transaction_quantity).
c. log: used to store operation logs, including fields such as log ID (log_id), operation type (log_type), and operation time (log_time).
Next, we can create a PHP class named "InventoryManagement" to manage inventory.
<?php class InventoryManagement { private $db; public function __construct($host, $username, $password, $database) { $this->db = new mysqli($host, $username, $password, $database); if ($this->db->connect_error) { die("连接数据库失败:" . $this->db->connect_error); } } public function getProductStock($product_id) { $query = "SELECT product_stock FROM products WHERE product_id = ?"; $stmt = $this->db->prepare($query); $stmt->bind_param("s", $product_id); $stmt->execute(); $stmt->bind_result($product_stock); $stmt->fetch(); $stmt->close(); return $product_stock; } public function addProductStock($product_id, $quantity) { $current_stock = $this->getProductStock($product_id); $new_stock = $current_stock + $quantity; $query = "UPDATE products SET product_stock = ? WHERE product_id = ?"; $stmt = $this->db->prepare($query); $stmt->bind_param("is", $new_stock, $product_id); $stmt->execute(); $stmt->close(); $this->logAction("Add Product Stock - Product ID: $product_id, Quantity: $quantity"); } public function reduceProductStock($product_id, $quantity) { $current_stock = $this->getProductStock($product_id); if ($current_stock < $quantity) { die("库存不足"); } $new_stock = $current_stock - $quantity; $query = "UPDATE products SET product_stock = ? WHERE product_id = ?"; $stmt = $this->db->prepare($query); $stmt->bind_param("is", $new_stock, $product_id); $stmt->execute(); $stmt->close(); $this->logAction("Reduce Product Stock - Product ID: $product_id, Quantity: $quantity"); } public function logAction($action) { $query = "INSERT INTO log (log_type, log_time) VALUES (?, NOW())"; $stmt = $this->db->prepare($query); $stmt->bind_param("s", $action); $stmt->execute(); $stmt->close(); } } ?>
Using this basic framework code, we can easily manage inventory. The following is the usage of some sample codes:
<?php // 创建库存管理对象 $inventory = new InventoryManagement("localhost", "root", "password", "inventory_management"); // 添加产品库存 $inventory->addProductStock("P001", 10); // 减少产品库存 $inventory->reduceProductStock("P001", 5); // 获取产品库存 $product_stock = $inventory->getProductStock("P001"); echo "Product Stock: $product_stock"; ?>
Through the above sample codes, we can clearly see how to use PHP to write the basic framework code of the inventory management system and perform basic inventory management operations.
Summary:
This article introduces how to use PHP to write the basic framework code of the inventory management system and provides some code examples. Through these codes, we can manage inventory more efficiently and implement basic increase and decrease inventory operations. Readers can expand and optimize the code according to actual needs to achieve more complex inventory management functions. Hope this article is helpful to you!
The above is the detailed content of How to use PHP to write the basic framework code of the inventory management system. For more information, please follow other related articles on the PHP Chinese website!