Current Location: Home> Latest Articles> PHP Inventory Management System Inventory Log Record Function Code Implementation

PHP Inventory Management System Inventory Log Record Function Code Implementation

M66 2025-06-13

Introduction

In an inventory management system, inventory log records are an important part of tracking and managing the changes in product inventory. This article will introduce how to write the inventory log record functionality for an inventory management system using PHP, with code examples to help readers understand and implement this feature.

1. Create the Database Table

First, we need to create a database table to store the inventory log records. Here is an example of a simple inventory log table structure:
CREATE TABLE IF NOT EXISTS inventory_logs (
  id int(11) NOT NULL AUTO_INCREMENT,
  product_id int(11) NOT NULL,
  action varchar(255) NOT NULL,
  quantity int(11) NOT NULL,
  created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

This table consists of the following fields:

  • id: The unique identifier for the inventory log record, using an auto-incrementing primary key.

  • product_id: The ID of the product, used to associate with a specific product.

  • action: The type of inventory action, such as "in" (stock in), "out" (stock out), etc.

  • quantity: The quantity of items involved in the action.

  • created_at: The timestamp when the record was created.

2. Writing the Inventory Log Record Code

Next, we'll write a PHP function for the inventory log record feature, using PDO to operate the database. Here's an example of the code:
class InventoryLog
{
    private $pdo;

    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function addLog($product_id, $action, $quantity)
    {
        $sql = "INSERT INTO inventory_logs (product_id, action, quantity) 
                VALUES (:product_id, :action, :quantity)";
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindValue(':product_id', $product_id, PDO::PARAM_INT);
        $stmt->bindValue(':action', $action, PDO::PARAM_STR);
        $stmt->bindValue(':quantity', $quantity, PDO::PARAM_INT);
        $stmt->execute();
    }

    public function getLogs($product_id)
    {
        $sql = "SELECT * FROM inventory_logs WHERE product_id = :product_id ORDER BY created_at DESC";
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindValue(':product_id', $product_id, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}

3. Usage Example

Here is an example of how to use this inventory log record feature:
$dbHost = 'localhost';
$dbName = 'inventory';
$dbUser = 'root';
$dbPassword = 'password';

$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$pdo = new PDO($dsn, $dbUser, $dbPassword);
$inventoryLog = new InventoryLog($pdo);

// Add an inventory log record
$inventoryLog->addLog(1, 'in', 10);

// Get inventory logs for product 1
$logs = $inventoryLog->getLogs(1);
foreach ($logs as $log) {
    echo "Product ID: " . $log['product_id'] . " Action Type: " . $log['action'] . " Quantity: " . $log['quantity'] . "<br>";
}

Conclusion

Through a simple code example, we have demonstrated how to implement the inventory log record functionality in a PHP-based inventory management system. Readers can expand and optimize it according to their actual needs. We hope this article helps readers understand and implement inventory log recording features, improving the efficiency of inventory management systems.