Implementing Logistics Tracking and Delivery Management in PHP for E-commerce
This tutorial explains how to build logistics tracking and delivery management features for an e‑commerce system using PHP, covering database table creation, inserting and updating records, and querying data to display order shipment status and delivery information.
In the context of modern e‑commerce, logistics tracking and delivery management are essential functions of a shopping system. This tutorial shows how to implement these features with PHP, providing code examples to help you understand and apply the concepts.
1. Logistics Tracking Implementation
1.1 Create Database Table
First, create a table named tracking in the e‑commerce database to store logistics tracking information. Example table structure:
CREATE TABLE tracking (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
order_id INT(11) NOT NULL,
status VARCHAR(255) NOT NULL,
update_time DATETIME
);1.2 Add Tracking Information
When an order status changes, insert a corresponding tracking record. The following code captures the order ID, status, and current time, then inserts the data into the tracking table.
// Get order ID and status
$order_id = $_POST['order_id'];
$status = $_POST['status'];
// Get current time
$update_time = date('Y-m-d H:i:s');
// Insert tracking information
$sql = "INSERT INTO tracking (order_id, status, update_time) VALUES ($order_id, '$status', '$update_time')";
$result = mysqli_query($conn, $sql);1.3 Query Tracking Information
To display an order's tracking details, query the tracking table and output each record.
// Get order ID
$order_id = $_POST['order_id'];
// Query tracking information
$sql = "SELECT * FROM tracking WHERE order_id = $order_id";
$result = mysqli_query($conn, $sql);
// Display tracking information
while ($row = mysqli_fetch_assoc($result)) {
echo $row['status'] . " - " . $row['update_time'] . "
";
}2. Delivery Management Implementation
2.1 Create Database Table
Create a delivery table to store delivery details such as courier and delivery time.
CREATE TABLE delivery (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
order_id INT(11) NOT NULL,
courier VARCHAR(255) NOT NULL,
delivery_time DATETIME
);2.2 Add Delivery Information
When an order is shipped, insert a delivery record with the courier name and shipment time.
// Get order ID, courier, and delivery time
$order_id = $_POST['order_id'];
$courier = $_POST['courier'];
$delivery_time = date('Y-m-d H:i:s');
// Insert delivery information
$sql = "INSERT INTO delivery (order_id, courier, delivery_time) VALUES ($order_id, '$courier', '$delivery_time')";
$result = mysqli_query($conn, $sql);2.3 Update Delivery Information
If the courier changes, update the corresponding record.
// Get order ID and new courier
$order_id = $_POST['order_id'];
$new_courier = $_POST['new_courier'];
// Update delivery information
$sql = "UPDATE delivery SET courier = '$new_courier' WHERE order_id = $order_id";
$result = mysqli_query($conn, $sql);2.4 Query Delivery Information
Retrieve and display delivery details for a specific order.
// Get order ID
$order_id = $_POST['order_id'];
// Query delivery information
$sql = "SELECT * FROM delivery WHERE order_id = $order_id";
$result = mysqli_query($conn, $sql);
// Display delivery information
while ($row = mysqli_fetch_assoc($result)) {
echo $row['courier'] . " - " . $row['delivery_time'] . "
";
}Conclusion
By following this tutorial, you have learned how to implement logistics tracking and delivery management using PHP. These features are crucial for enhancing user experience and operational efficiency in e‑commerce platforms, and mastering them helps you build a more robust online shopping system.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.