The second-hand recycling website developed by PHP realizes real-time updating of product information
With social development and changes in consumption concepts, the second-hand recycling market has attracted more and more attention. In order to meet users' needs for second-hand goods, many websites have begun to emerge. This article will introduce how to use PHP to develop a second-hand recycling website and implement the function of instantly updating product information.
1. Create database and data table
First, we need to create a database to store the product information uploaded by users. Open the MySQL database and execute the following SQL statement to create a database named "recycle":
CREATE DATABASE recycle;
Then, switch to the "recycle" database and execute the following SQL statement to create a data table named "products" :
USE recycle; CREATE TABLE products ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, description TEXT NOT NULL, price DECIMAL(10, 2) NOT NULL, image VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
2. Design the page structure of the website
Create a file named "index.php" and write HTML code in the file to design the page structure of the website. The following is a simple example:
<!DOCTYPE html> <html> <head> <title>二手物品回收</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <h1>二手物品回收</h1> <form action="upload.php" method="POST" enctype="multipart/form-data"> <label for="name">物品名称</label> <input type="text" name="name" id="name"> <label for="description">物品描述</label> <textarea name="description" id="description"></textarea> <label for="price">价格</label> <input type="text" name="price" id="price"> <label for="image">上传图片</label> <input type="file" name="image" id="image"> <input type="submit" value="上传"> </form> <h2>最新物品</h2> <?php // PHP代码将在下面添加 ?> </div> </body> </html>
3. Processing uploaded product information
Create a file named "upload.php" and write PHP code in the file to process the uploaded product information. Product information. The following is a simple example:
<?php // 获取用户上传的商品信息 $name = $_POST['name']; $description = $_POST['description']; $price = $_POST['price']; // 处理上传的图片 $image = $_FILES['image']['name']; $image_tmp = $_FILES['image']['tmp_name']; $target_dir = "uploads/"; $target_file = $target_dir . basename($image); move_uploaded_file($image_tmp, $target_file); // 将商品信息插入到数据库中 $pdo = new PDO("mysql:host=localhost;dbname=recycle", "root", ""); $sql = "INSERT INTO products (name, description, price, image) VALUES (?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute([$name, $description, $price, $target_file]); // 重定向到首页 header("location: index.php"); exit; ?>
4. Display the latest item information
Return to the "index.php" file and insert the following code at the location marked by the PHP code to display the latest uploaded items Information:
<?php // 查询最新上传的物品信息 $sql = "SELECT * FROM products ORDER BY created_at DESC LIMIT 10"; $stmt = $pdo->prepare($sql); $stmt->execute(); $products = $stmt->fetchAll(); // 显示最新物品信息 foreach ($products as $product) { echo '<div class="product">'; echo '<img src="' . $product['image'] . '">'; echo '<h3>' . $product['name'] . '</h3>'; echo '<p>' . $product['description'] . '</p>'; echo '<p class="price">价格:' . $product['price'] . '</p>'; echo '</div>'; } ?>
5. Improve the style of the website
Create a file named "style.css" and write CSS code in the file to beautify the style of the website. The following is a simple example:
.container { max-width: 800px; margin: 0 auto; } h1, h2, h3 { text-align: center; } form { margin-bottom: 20px; } label, input, textarea { display: block; margin-bottom: 10px; } .product { display: inline-block; width: 200px; margin: 10px; border: 1px solid #ccc; padding: 10px; text-align: center; } .product img { max-width: 100%; } .price { font-weight: bold; }
6. Test website
Now, open the homepage "index.php" of the website, try to upload some product information and check whether the latest items are displayed normally. .
At this point, the second-hand recycling website we developed using PHP is completed. Users can upload information about second-hand products through the website and realize the function of instantly updating product information. I hope this article can help you in PHP development.
The above is the detailed content of The second-hand recycling website developed by PHP realizes real-time updating of product information. For more information, please follow other related articles on the PHP Chinese website!