Home  >  Article  >  Backend Development  >  How to use PHP inventory management system to optimize product management

How to use PHP inventory management system to optimize product management

WBOY
WBOYOriginal
2023-08-18 23:49:451513browse

How to use PHP inventory management system to optimize product management

How to use PHP inventory management system to optimize product management

Inventory management is a very important part of business operations. An excellent inventory management system can help companies reduce inventory cost, improve inventory turnover, and avoid excess or insufficient inventory. As a popular programming language, PHP has a wide range of applications and is particularly suitable for developing inventory management systems. This article will introduce how to use PHP to develop an inventory management system and demonstrate the key functions through code examples.

  1. Create database

First, we need to create a database in MySQL and create the necessary tables to store product information and inventory information. The following is the sample code for the table:

CREATE DATABASE inventory_management;

USE inventory_management;

CREATE TABLE products (
   id INT(11) PRIMARY KEY AUTO_INCREMENT,
   name VARCHAR(100) NOT NULL,
   price DECIMAL(10,2) NOT NULL
);

CREATE TABLE stock (
   id INT(11) PRIMARY KEY AUTO_INCREMENT,
   product_id INT(11),
   quantity INT(11) NOT NULL,
   FOREIGN KEY (product_id) REFERENCES products(id)
);
  1. PHP connects to the database

Next, we use PHP to connect to the database and establish a connection with MySQL. The following is a sample code to connect to the database:

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "inventory_management";

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

if ($conn->connect_error) {
   die("连接失败: " . $conn->connect_error);
}
?>
  1. Product management function

Next, we will implement some key product management functions, including adding new products and editing products and delete items. Here is the sample code:

<?php
// 添加新商品
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["add_product"])) {
   $name = $_POST["name"];
   $price = $_POST["price"];

   $sql = "INSERT INTO products (name, price) VALUES ('$name', '$price')";

   if ($conn->query($sql) === TRUE) {
      echo "商品添加成功";
   } else {
      echo "商品添加失败:" . $conn->error;
   }
}
?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
   <label>商品名称:</label>
   <input type="text" name="name" required>
   <br>
   <label>商品价格:</label>
   <input type="number" name="price" step="0.01" required>
   <br>
   <input type="submit" name="add_product" value="添加商品">
</form>

<?php
// 编辑商品
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit_product"])) {
   $id = $_POST["id"];
   $name = $_POST["name"];
   $price = $_POST["price"];

   $sql = "UPDATE products SET name='$name', price='$price' WHERE id='$id'";

   if ($conn->query($sql) === TRUE) {
      echo "商品更新成功";
   } else {
      echo "商品更新失败:" . $conn->error;
   }
}
?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
   <input type="hidden" name="id" value="<?php echo $product_id; ?>">
   <label>商品名称:</label>
   <input type="text" name="name" value="<?php echo $product_name; ?>" required>
   <br>
   <label>商品价格:</label>
   <input type="number" name="price" step="0.01" value="<?php echo $product_price; ?>" required>
   <br>
   <input type="submit" name="edit_product" value="更新商品">
</form>

<?php
// 删除商品
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete_product"])) {
   $id = $_POST["id"];

   $sql = "DELETE FROM products WHERE id='$id'";

   if ($conn->query($sql) === TRUE) {
      echo "商品删除成功";
   } else {
      echo "商品删除失败:" . $conn->error;
   }
}
?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
   <input type="hidden" name="id" value="<?php echo $product_id; ?>">
   <input type="submit" name="delete_product" value="删除商品">
</form>
  1. Inventory management functions

Finally, we will implement some key inventory management functions, including adding inventory, reducing inventory, and querying inventory. The following is a sample code:

<?php
// 添加库存
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["add_stock"])) {
   $product_id = $_POST["product_id"];
   $quantity = $_POST["quantity"];

   $sql = "INSERT INTO stock (product_id, quantity) VALUES ('$product_id', '$quantity')";

   if ($conn->query($sql) === TRUE) {
      echo "库存添加成功";
   } else {
      echo "库存添加失败:" . $conn->error;
   }
}
?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
   <label>商品ID:</label>
   <input type="number" name="product_id" required>
   <br>
   <label>添加数量:</label>
   <input type="number" name="quantity" required>
   <br>
   <input type="submit" name="add_stock" value="添加库存">
</form>

<?php
// 减少库存
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["reduce_stock"])) {
   $product_id = $_POST["product_id"];
   $quantity = $_POST["quantity"];

   $sql = "UPDATE stock SET quantity = quantity - '$quantity' WHERE product_id='$product_id'";

   if ($conn->query($sql) === TRUE) {
      echo "库存减少成功";
   } else {
      echo "库存减少失败:" . $conn->error;
   }
}
?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
   <label>商品ID:</label>
   <input type="number" name="product_id" required>
   <br>
   <label>减少数量:</label>
   <input type="number" name="quantity" required>
   <br>
   <input type="submit" name="reduce_stock" value="减少库存">
</form>

<?php
// 查询库存
$sql = "SELECT products.name, stock.quantity FROM products INNER JOIN stock ON products.id = stock.product_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
   while($row = $result->fetch_assoc()) {
      echo "商品名称:" . $row["name"] . ",库存数量:" . $row["quantity"] . "<br>";
   }
} else {
   echo "无库存记录";
}
?>

Through the above code examples, we have implemented the functions of adding, editing and deleting products, as well as the functions of adding, reducing and querying inventory. These functions can help us better manage product inventory and optimize business operation efficiency.

To sum up, this article introduces how to use PHP to develop an inventory management system and shows key functional code examples. Through this system, companies can manage product inventory more efficiently, reduce inventory costs, and improve operational efficiency.

The above is the detailed content of How to use PHP inventory management system to optimize product management. 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