Detailed explanation of the mall order tracking function developed with PHP
With the rapid development of e-commerce, more and more merchants choose to sell products online. For these merchants, the order tracking function is very important because it can provide customers with accurate order status and delivery information. In this article, we will introduce in detail how to use PHP to develop a simple yet powerful shopping mall order tracking system, and attach corresponding code examples.
First, we need to create a table in the database to store order information. The following is a simple example table structure:
CREATE TABLE orders ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, order_number VARCHAR(20), status VARCHAR(20), shipping_address VARCHAR(100), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
In this example, we use an auto-incremented id as the primary key and store the user id, order number, order status, shipping address and order creation time and update time.
Next, we need to create an order tracking function in our code. First, we need a page to display the order list. The following is a simple PHP code example:
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 检查数据库连接 if(mysqli_connect_errno()){ echo '数据库连接失败:' . mysqli_connect_error(); exit(); } // 查询订单列表 $result = mysqli_query($conn, "SELECT * FROM orders"); // 输出订单列表 while($row = mysqli_fetch_assoc($result)){ echo '订单号:' . $row['order_number'] . '<br>'; echo '订单状态:' . $row['status'] . '<br>'; echo '配送地址:' . $row['shipping_address'] . '<br>'; echo '创建时间:' . $row['created_at'] . '<br>'; echo '更新时间:' . $row['updated_at'] . '<br><br>'; } // 关闭数据库连接 mysqli_close($conn); ?>
Through the above code, we can query the order information in the database and output it one by one to display on the page.
Then we need a page to display the details of a specific order. Here is a simple code example:
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 检查数据库连接 if(mysqli_connect_errno()){ echo '数据库连接失败:' . mysqli_connect_error(); exit(); } // 获取订单id $order_id = $_GET['id']; // 查询订单信息 $result = mysqli_query($conn, "SELECT * FROM orders WHERE id = $order_id"); // 输出订单详细信息 if(mysqli_num_rows($result) > 0){ $row = mysqli_fetch_assoc($result); echo '订单号:' . $row['order_number'] . '<br>'; echo '订单状态:' . $row['status'] . '<br>'; echo '配送地址:' . $row['shipping_address'] . '<br>'; echo '创建时间:' . $row['created_at'] . '<br>'; echo '更新时间:' . $row['updated_at'] . '<br>'; } else { echo '未找到该订单信息'; } // 关闭数据库连接 mysqli_close($conn); ?>
With this code, we can get the id of a specific order and query the database for the details of that order and display it on the page.
Finally, we can also add a function to query the order status on the order page. The following is a simple code example:
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 检查数据库连接 if(mysqli_connect_errno()){ echo '数据库连接失败:' . mysqli_connect_error(); exit(); } // 获取订单号 $order_number = $_POST['order_number']; // 查询订单状态 $result = mysqli_query($conn, "SELECT status FROM orders WHERE order_number = '$order_number'"); // 输出订单状态 if(mysqli_num_rows($result) > 0){ $row = mysqli_fetch_assoc($result); echo '订单状态:' . $row['status']; } else { echo '未找到该订单信息'; } // 关闭数据库连接 mysqli_close($conn); ?>
Through this code, we can get the order number entered by the user, query the status of the order in the database, and display it on the page.
Through the above code examples, we can see how to use PHP to develop a simple and powerful shopping mall order tracking system. Of course, this is just a basic example, and you can expand and optimize the functions according to your own needs. Hope this article helps you!
The above is the detailed content of Detailed explanation of the mall order tracking function developed with PHP. For more information, please follow other related articles on the PHP Chinese website!