Product inventory management is an essential task for any business. Using a PHP-based automated inventory management tool can significantly improve the efficiency and accuracy of inventory tracking. This article explains how to develop a simple yet effective product inventory management tool using PHP, with accompanying code examples for reference.
Before starting the development process, we need to define the objectives and feature requirements. A basic product inventory management tool should include the following key functionalities:
Based on the aforementioned requirements, we need to design a simple database. We will create a database named 'inventory_management' containing two core tables: a product table (products) and a sales record table (sales).
<?php
$host = "localhost";
$dbname = "inventory_management";
$username = "root";
$password = "";
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
?>
<?php
// Add a product
function addProduct($name, $price, $quantity) {
global $pdo;
$stmt = $pdo->prepare("INSERT INTO products (name, price, quantity) VALUES (?, ?, ?)");
$stmt->execute([$name, $price, $quantity]);
return $pdo->lastInsertId();
}
// Update product details
function updateProduct($id, $name, $price, $quantity) {
global $pdo;
$stmt = $pdo->prepare("UPDATE products SET name = ?, price = ?, quantity = ? WHERE id = ?");
$stmt->execute([$name, $price, $quantity, $id]);
return $stmt->rowCount();
}
// Delete a product
function deleteProduct($id) {
global $pdo;
$stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
$stmt->execute([$id]);
return $stmt->rowCount();
}
?>
<?php
// Update stock quantity
function updateQuantity($id, $quantity) {
global $pdo;
$stmt = $pdo->prepare("UPDATE products SET quantity = ? WHERE id = ?");
$stmt->execute([$quantity, $id]);
return $stmt->rowCount();
}
?>
<?php
// Add a sales record
function addSale($productId, $quantity, $saleDate) {
global $pdo;
$stmt = $pdo->prepare("INSERT INTO sales (product_id, quantity, sale_date) VALUES (?, ?, ?)");
$stmt->execute([$productId, $quantity, $saleDate]);
return $pdo->lastInsertId();
}
?>
Through the above code examples, it's clear that developing a simple PHP-based automated product inventory management tool is not difficult. This tool can greatly improve the efficiency and accuracy of a business's operations, while also providing comprehensive reporting features to assist with sales analysis and forecasting. Of course, in a real-world application, further customization and optimization may be necessary to meet specific business needs.
We hope this article helps you understand how to develop an automated product inventory management tool using PHP.