Current Location: Home> Latest Articles> PHP Inventory Tracking and Management: A Guide to Implementing Stock Traceability Function

PHP Inventory Tracking and Management: A Guide to Implementing Stock Traceability Function

M66 2025-07-03

PHP Inventory Tracking and Management: A Guide to Implementing Stock Traceability Function

Introduction

With the rapid growth of e-commerce, product inventory management has become a core task for e-commerce platforms and physical stores. Being able to track inventory in real time, avoid stockouts or excess inventory, is crucial for improving operational efficiency. This article will guide you on how to use PHP and MySQL to implement product inventory tracking and traceability functionality.

Creating the Database Table

First, we need to create a database table called “products” to store product information and inventory data. You can use the following SQL statement to create the table:

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    quantity INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Connecting to the Database

Next, we need to write PHP code to connect to the MySQL database. Here's a simple example of how to do this:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

Adding Products to the Database

Using the following PHP code, you can add product information and stock quantity to the database:

$name = "iPhone 12";
$quantity = 10;

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

if (mysqli_query($conn, $sql)) {
    echo "Product added successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

Updating Product Inventory

If the inventory quantity changes, you can use the following code to update the product's stock:

$id = 1;
$quantity = 5;

$sql = "UPDATE products SET quantity = $quantity WHERE id = $id";

if (mysqli_query($conn, $sql)) {
    echo "Product quantity updated successfully";
} else {
    echo "Error updating product quantity: " . mysqli_error($conn);
}

Querying Product Inventory

The following code can be used to query the inventory of a specific product in the database:

$sql = "SELECT * FROM products WHERE id = $id";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    echo "Product: " . $row["name"] . "<br>";
    echo "Quantity: " . $row["quantity"] . "<br>";
} else {
    echo "Product not found";
}

Deleting Products

If you need to delete a product, you can use the following code to remove the product from the database:

$id = 1;

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

if (mysqli_query($conn, $sql)) {
    echo "Product deleted successfully";
} else {
    echo "Error deleting product: " . mysqli_error($conn);
}

Closing the Database Connection

After the operations are completed, remember to close the database connection:

<span class="fun">mysqli_close($conn);</span>

Conclusion

By using the PHP code examples provided, we can implement a simple product inventory management system that helps e-commerce platforms track product stock in real-time, as well as query, update, and delete product information. This management approach not only improves inventory control efficiency but also prevents stock piling or out-of-stock situations.