How to use PHP Developer City to realize product collection function
With the development of the Internet, e-commerce has become the main channel for people to shop. In a mall, users can browse various products and select their favorite products for purchase. However, sometimes users may encounter some problems, such as finding a favorite product but not being able to purchase it immediately, and hoping to save it for later viewing and purchase. At this time, the product collection function becomes very important.
This article will introduce how to use PHP Developer City to realize the product collection function.
First, we need to create a database table to store the user's collection of products. The table can contain the following fields: user ID, product ID, and adding time. The user ID will be used to uniquely identify the user, the product ID will be used to uniquely identify the product, and the adding time will be used to record the time when the user added the product.
Next, we need to add a "Collection" button on the product details page of the mall. When the user clicks the button, we need to save the user ID, product ID, and adding time to the collection table.
In PHP, we can implement this function through the following code:
<?php // 连接数据库 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "dbname"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取用户ID和商品ID $userId = $_SESSION['userId']; $productId = $_GET['productId']; $currentTime = date('Y-m-d H:i:s'); // 将用户ID、商品ID和添加时间保存到收藏表中 $sql = "INSERT INTO collection (userId, productId, addTime) VALUES ('$userId', '$productId', '$currentTime')"; if ($conn->query($sql) === TRUE) { echo "商品已成功添加到收藏夹!"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
When the user clicks the "Collection" button, the PHP code will save the user ID, product ID and adding time to in the favorites table, and returns a prompt message indicating successful saving.
Next, we need to display the user’s favorite product list on the user’s personal center page. In this page, we can obtain the product information collected by the user by querying the collection table and display it.
In PHP, we can implement this function through the following code:
<?php // 连接数据库 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "dbname"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取用户ID $userId = $_SESSION['userId']; // 查询收藏表获取用户收藏的商品信息 $sql = "SELECT * FROM collection WHERE userId = '$userId'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出每一条收藏的商品信息 while($row = $result->fetch_assoc()) { echo "商品ID: " . $row["productId"]. " - 添加时间: " . $row["addTime"]. "<br>"; } } else { echo "您还没有收藏任何商品!"; } $conn->close(); ?>
The above PHP code will query the collection table and output the product ID and addition time of the user's collection.
Through the above operations, we successfully developed a mall using PHP and implemented the product collection function. Users can click the "Collect" button to collect their favorite products and view their collected products on the personal center page.
Of course, this is just a simple implementation, and there are many details that can be improved and expanded. For example, we can add the function of canceling favorites, allowing users to cancel favorites of a certain product; we can also display favorites in the form of thumbnails, allowing users to view favorites more intuitively on the personal center page.
I hope this article will help you understand how to use the PHP Developer City to realize the product collection function!
The above is the detailed content of PHP developer city to realize product collection function. For more information, please follow other related articles on the PHP Chinese website!