PHP實現商品庫存追蹤與追溯的功能指南
引言:
隨著電子商務的快速發展,商品庫存管理成為了一項必不可少的任務。對於電商平台或實體店面來說,能夠準確追蹤商品庫存的情況非常重要,能夠幫助其有效地管理庫存、避免售罄和庫存過剩的情況發生。本文將介紹如何使用PHP語言實現商品庫存追蹤與追溯的功能。
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 );
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
$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); }
$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); }
$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"; }
$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); }
mysqli_close($conn);
透過以上的程式碼範例,我們可以實現商品庫存追蹤與追溯的功能。使用PHP語言操作資料庫,可以幫助我們管理商品的庫存數量,方便地查詢、更新和刪除商品資訊。有效的庫存管理可以提高商品生命週期的可控性和銷售效率,為電商平台和實體店面帶來更好的營運效果。
以上是PHP實現商品庫存追蹤與追溯的功能指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!