Second-hand recycling website developed in PHP provides free product publishing function

王林
Release: 2023-07-01 13:08:01
Original
541 people have browsed it

The second-hand recycling website developed by PHP provides free product publishing functions

With the improvement of people's awareness of environmental protection and the continuous expansion of the second-hand goods market, second-hand recycling websites have gradually attracted people's attention. In order to meet the needs of users, this website is developed through PHP and provides a free product publishing function, allowing users to easily publish second-hand products that they no longer use, providing convenience to other people in need.

This article will introduce how to use PHP to develop a simple second-hand recycling website, and provide code examples for product release.

First, we need to create a database to store product information. You can use MySQL to create a database named "second_hand" and create a table named "goods" to store product information. The table can contain the following fields: id, title, price, description and image.

Next, we create a file named "index.php" as the homepage of the website. In this file, we first connect to the database and query all product information. Then, use a loop to display each product information on the page. The code example is as follows:

<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "second_hand");

// 检查连接是否成功
if (!$conn) {
    die("数据库连接失败: " . mysqli_connect_error());
}

// 查询所有商品信息
$sql = "SELECT * FROM goods";
$result = mysqli_query($conn, $sql);

// 判断是否有商品信息
if (mysqli_num_rows($result) > 0) {
    // 循环输出商品信息
    while($row = mysqli_fetch_assoc($result)) {
        echo "商品标题: " . $row["title"]. " - 价格: " . $row["price"]. "<br>";
        echo "商品描述: " . $row["description"]. "<br><br>";
    }
} else {
    echo "暂无商品信息";
}

// 关闭数据库连接
mysqli_close($conn);
?>
Copy after login

After displaying all product information on the homepage, we create a file named "post.php" for users to publish products. The file will contain a form where users can fill in the product's title, price, description and upload product images. The code example is as follows:

<form action="post.php" method="post" enctype="multipart/form-data">
    商品标题: <input type="text" name="title"><br><br>
    价格: <input type="text" name="price"><br><br>
    描述: <textarea name="description"></textarea><br><br>
    图片: <input type="file" name="image"><br><br>
    <input type="submit" value="发布">
</form>

<?php
// 处理商品发布逻辑
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 连接数据库
    $conn = mysqli_connect("localhost", "username", "password", "second_hand");
    
    // 检查连接是否成功
    if (!$conn) {
        die("数据库连接失败: " . mysqli_connect_error());
    }
    
    // 获取用户输入的商品信息
    $title = $_POST["title"];
    $price = $_POST["price"];
    $description = $_POST["description"];
    
    // 上传图片
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
    
    // 添加商品信息到数据库
    $sql = "INSERT INTO goods (title, price, description, image) VALUES ('$title', '$price', '$description', '$target_file')";
    
    if (mysqli_query($conn, $sql)) {
        echo "商品发布成功";
    } else {
        echo "商品发布失败: " . mysqli_error($conn);
    }
    
    // 关闭数据库连接
    mysqli_close($conn);
}
?>
Copy after login

Through the above code example, we can implement a simple second-hand recycling website and provide free product publishing function. Users only need to fill in the relevant information of the product and upload pictures to publish second-hand products that they no longer use, thereby providing convenience to other people in need.

It should be noted that the above sample code is for demonstration purposes only. In actual projects, some security and legality verifications need to be done, and appropriate error handling mechanisms must be added.

I hope this article can help readers who are interested in PHP development and provide inspiration and reference.

The above is the detailed content of Second-hand recycling website developed in PHP provides free product publishing function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!