Implementing Logistics Tracking and Delivery Management in PHP for E-commerce
This tutorial explains how to build essential logistics tracking and delivery management features for an e‑commerce system using PHP, covering database schema design, code for inserting, updating, and querying tracking and delivery records, and providing complete example snippets.
In the context of modern e‑commerce, logistics tracking and delivery management are essential functions of a shopping system. This article teaches how to implement these features using PHP, providing code examples to help you understand and apply them.
1. Implementation of Logistics Tracking Function
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
);2) Add logistics tracking information
When an order status changes, insert a corresponding tracking record. The following code captures the order ID and status from a POST request, records the current time, and 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);3) Query logistics tracking information
To display an order's tracking history, query the tracking table for the given order ID and output each record's status and timestamp.
// 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. Implementation of Delivery Management Function
1) Create database table
Create a delivery table to store delivery details such as courier and delivery time. Example schema:
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) Add delivery information
When an order is shipped, insert a delivery record with the order ID, selected courier, and the current timestamp.
// 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);3) Update delivery information
If the courier needs to be changed, update the corresponding record in the delivery table.
// 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);4) Query delivery information
To view an order's delivery details, query the delivery table and output the courier and delivery time.
// 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 features using PHP. These functionalities are crucial for enhancing user experience and operational efficiency in e‑commerce systems, and mastering them will help you build more robust online stores.
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.