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.
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);
}
}
$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>";
}