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 platform using PHP, covering database table creation, inserting, updating, and querying of tracking and delivery records with complete code examples.
In the context of modern e‑commerce, logistics tracking and delivery management are essential functions of a shopping system. This article shows how to implement these features with PHP and provides code examples to help you understand and apply them.
1. Implementing Logistics Tracking
1) Create database table
First, create a tracking table in the shop database to store logistics information. Example 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 tracking information
When an order status changes, insert a corresponding tracking record. Sample code:
// 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 info
$sql = "INSERT INTO tracking (order_id, status, update_time) VALUES ($order_id, '$status', '$update_time')";
$result = mysqli_query($conn, $sql);3) Query tracking information
To display tracking data for an order, query the table and output the results. Sample code:
// Get order ID
$order_id = $_POST['order_id'];
// Query tracking info
$sql = "SELECT * FROM tracking WHERE order_id = $order_id";
$result = mysqli_query($conn, $sql);
// Display tracking info
while ($row = mysqli_fetch_assoc($result)) {
echo $row['status'] . " - " . $row['update_time'] . "
";
}2. Implementing Delivery Management
1) Create database table
Create a delivery table to store delivery details. Example structure:
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. Sample code:
// 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 info
$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 changes, update the record. Sample code:
// Get order ID and new courier
$order_id = $_POST['order_id'];
$new_courier = $_POST['new_courier'];
// Update delivery info
$sql = "UPDATE delivery SET courier = '$new_courier' WHERE order_id = $order_id";
$result = mysqli_query($conn, $sql);4) Query delivery information
To view delivery details for an order, query and display the data. Sample code:
// Get order ID
$order_id = $_POST['order_id'];
// Query delivery info
$sql = "SELECT * FROM delivery WHERE order_id = $order_id";
$result = mysqli_query($conn, $sql);
// Display delivery info
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 systems, and mastering them helps you build more complete online shopping platforms.
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.