Current Location: Home> Latest Articles> PHP Inventory Management System Inventory Correction Function Code Example

PHP Inventory Management System Inventory Correction Function Code Example

M66 2025-06-15

PHP Inventory Management System Inventory Correction Function Code Example

In an inventory management system, the inventory correction function is crucial. It helps administrators adjust inventory quantities in real-time to ensure data accuracy. This article will show you how to implement the inventory correction function using PHP example code.

Creating the Database Table Structure

Before we begin writing the code, we need to create a database and the corresponding inventory table. The table will include fields like product ID, product name, and quantity. Below is the SQL statement to create the table:

CREATE TABLE inventory (
    id INT(10) AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    quantity INT(10)
);

PHP Code for Implementing the Inventory Correction Function

Next, we will write PHP code to implement the inventory correction function:


<?php
// Connect to the database
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get POST data
$id = $_POST["id"];
$quantity = $_POST["quantity"];

// Check if the product ID exists
$sql = "SELECT * FROM inventory WHERE id = " . $id;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $currentQuantity = $row["quantity"];
    
    // Update the inventory quantity
    $newQuantity = $currentQuantity + $quantity;
    
    // Execute the update statement
    $updateSql = "UPDATE inventory SET quantity = " . $newQuantity . " WHERE id = " . $id;
    
    if ($conn->query($updateSql) === TRUE) {
        echo "Inventory correction successful";
    } else {
        echo "Inventory correction failed: " . $conn->error;
    }
} else {
    echo "Product ID does not exist";
}

$conn->close();
?>

Code Explanation

In the above code, we first use the mysqli object to connect to the database and retrieve the product ID and correction quantity from the POST request. Then, we execute a SELECT query to check if the product exists in the inventory table. If the product exists, the code reads the current inventory quantity, calculates the new quantity, and updates it in the database using an UPDATE statement.

Summary and Important Notes

It’s important to note that the example code here is for demonstration purposes only. In a real-world application, you should implement additional security measures and error handling to ensure system stability and security.

With this example, you can easily learn how to implement the inventory correction function using PHP. This will not only help you manage your inventory but also improve work efficiency. You can further customize the code to better meet the specific needs of your business.