Home >Backend Development >PHP Tutorial >Second-hand recycling website developed using PHP supports batch publishing function

Second-hand recycling website developed using PHP supports batch publishing function

WBOY
WBOYOriginal
2023-07-01 13:29:061263browse

The second-hand recycling website developed using PHP supports the batch publishing function

With the continuous changes in the social environment, the environmental protection industry of second-hand recycling is also gradually emerging. In order to facilitate users to publish their second-hand items for more efficient recycling, it is necessary to create a second-hand recycling website that supports batch publishing functions. This article will introduce how to use PHP to develop such a website and provide relevant code examples.

1. Build a website framework

To use PHP to develop a second-hand recycling website, you must first build a stable website structure. The following is a simple example:

<!-- index.php -->
<!DOCTYPE html>
<html>
<head>
    <title>二手回收网站</title>
</head>
<body>
    <header>
        <h1>二手回收网站</h1>
    </header>

    <nav>
        <ul>
            <li><a href="index.php">首页</a></li>
            <li><a href="publish.php">发布</a></li>
            <li><a href="search.php">搜索</a></li>
        </ul>
    </nav>

    <main>
        <!-- 主要内容 -->
    </main>

    <footer>
        版权所有 &copy; 二手回收网站
    </footer>
</body>
</html>

The above code defines a basic website architecture, including header, navigation bar, main content and bottom.

2. Implement the batch publishing function

Adding the batch publishing function on the publishing page can help users publish multiple second-hand items at one time and improve user efficiency. The following is a simple example:

<!-- publish.php -->
<!DOCTYPE html>
<html>
<head>
    <title>发布</title>
</head>
<body>
    <h2>发布二手物品</h2>

    <form action="publish.php" method="post" enctype="multipart/form-data">
        <label for="title">标题:</label>
        <input type="text" id="title" name="title" required><br><br>

        <label for="description">描述:</label>
        <input type="text" id="description" name="description" required><br><br>

        <label for="price">价格:</label>
        <input type="text" id="price" name="price" required><br><br>

        <label for="images[]">图片:</label>
        <input type="file" id="images[]" name="images[]" multiple required><br><br>

        <input type="submit" value="发布">
    </form>

    <?php
    if(isset($_POST['title']) && isset($_POST['description']) && isset($_POST['price'])){
        $title = $_POST['title'];
        $description = $_POST['description'];
        $price = $_POST['price'];

        // 保存图片
        $images = $_FILES['images']['tmp_name'];
        foreach($images as $image){
            move_uploaded_file($image, "uploads/" . basename($image));
        }

        // 将物品信息存入数据库
        $connection = new mysqli("localhost", "username", "password", "database");
        if($connection->connect_error){
            die("数据库连接失败:" . $connection->connect_error);
        }
        $sql = "INSERT INTO items (title, description, price) VALUES ('$title', '$description', '$price')";
        if($connection->query($sql)){
            echo "发布成功!";
        }else{
            echo "发布失败:" . $connection->error;
        }
        $connection->close();
    }
    ?>
</body>
</html>

The above code defines a publishing page and uses PHP's $_POST and $_FILES global variables to receive form submission data. After saving the image to the folder specified by the server, the item information is stored in the database.

Through the above sample code, we can implement a second-hand recycling website that supports batch publishing function. Users can publish multiple items at one time, improving publishing efficiency. Of course, this is just a simple example, and more functions and pages are needed to develop a complete second-hand recycling website. I hope this article can provide you with some development ideas and references.

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

Statement:
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