Home >Backend Development >PHP Tutorial >The second-hand recycling website developed by PHP realizes the user's historical order viewing function
The second-hand recycling website developed in PHP realizes the function of viewing historical orders for users
With the growth of the second-hand recycling market and the increase in user transaction volume, a fully functional second-hand recycling website has become more and more important. In order to improve the user experience, we can increase the usability and convenience of the website by implementing the user's historical order viewing function. This article will introduce how to use PHP development to implement this function and provide corresponding code examples.
First, we need to design the database table structure to store information related to user orders. Suppose we need to store information such as order number (order_id), user ID (user_id), order status (status), and order amount (amount). We can design a table named orders.
The structure of the orders table is as follows:
CREATE TABLE orders ( order_id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, status VARCHAR(20), amount DECIMAL(10,2) );
After the user generates an order on the second-hand recycling website, we need to save the order information into the database. On the same page where the order is generated, we can add a "View Historical Orders" button to jump to the historical order page.
Add the following HTML code on the order generation page:
<button onclick="location.href='history_orders.php'">查看历史订单</button>
On the historical order page, we need to extract data from the database Get all the historical orders of this user and display them.
First, create a file named history_orders.php
and add the following PHP code:
<?php // 连接数据库 $host = 'localhost'; $db = 'your_database'; $user = 'your_username'; $password = 'your_password'; $conn = mysqli_connect($host, $user, $password, $db); if (!$conn) { die('数据库连接失败: ' . mysqli_connect_error()); } // 获取当前用户ID $user_id = $_SESSION['user_id']; // 查询历史订单 $sql = "SELECT * FROM orders WHERE user_id = $user_id"; $result = mysqli_query($conn, $sql); // 判断是否有历史订单 if (mysqli_num_rows($result) > 0) { echo "<table>"; echo "<tr><th>订单编号</th><th>订单状态</th><th>订单金额</th></tr>"; while ($row = mysqli_fetch_assoc($result)) { echo "<tr><td>".$row['order_id']."</td><td>".$row['status']."</td><td>".$row['amount']."</td></tr>"; } echo "</table>"; } else { echo "没有历史订单."; } // 关闭数据库连接 mysqli_close($conn); ?>
After completing the above code, place the history_orders.php
file in the same directory as other pages, and ensure the correctness of the database connection information.
Now, on the second-hand recycling website, when users click the "View Historical Orders" button, they will be redirected to the historical order page and display all their historical order information. If there is no historical order, the corresponding prompt message will be displayed.
By implementing the user's historical order viewing function, we can improve the user experience of the second-hand recycling website and increase users' trust and convenience in the website. The above example code can be used as a starting point for developing this feature, which you can modify and extend according to your specific needs. Hope this article helps you!
The above is the detailed content of The second-hand recycling website developed by PHP realizes the user's historical order viewing function. For more information, please follow other related articles on the PHP Chinese website!