Second-hand recycling website developed in PHP provides product evaluation and comment functions

王林
Release: 2023-07-02 19:58:01
Original
998 people have browsed it

The second-hand recycling website developed by PHP provides product evaluation and comment functions

With the continuous expansion and development of the second-hand trading market, more and more people choose to buy second-hand goods. In this process, people hope to understand the true condition of the product and the purchasing experience of others in order to make more informed purchasing decisions. Therefore, adding product evaluation and comment functions to second-hand recycling websites is a very important step.

In PHP development, we can implement product evaluation and comment functions through database and front-end technology. We first create a MySQL database, including a product table and a review table, as shown below:

Product table (items):

CREATE TABLE items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    description TEXT,
    price DECIMAL(10, 2)
);
Copy after login

Comments table (reviews):

CREATE TABLE reviews (
    id INT AUTO_INCREMENT PRIMARY KEY,
    item_id INT,
    rating INT,
    comment TEXT,
    FOREIGN KEY (item_id) REFERENCES items(id)
);
Copy after login

Next, we add the HTML form and PHP processing code for evaluation comments on the product details page of the second-hand recycling website. The HTML form is used for users to enter ratings and comments, and the PHP code is responsible for saving the data entered by users into the comments table.

HTML form:

<form action="add_review.php" method="POST">
    <label for="rating">评分:</label>
    <input type="number" name="rating" min="1" max="5" required><br>

    <label for="comment">评论:</label><br>
    <textarea name="comment" rows="4" cols="50" required></textarea><br>

    <input type="hidden" name="item_id" value="<?php echo $item_id; ?>">
    <input type="submit" value="提交评论">
</form>
Copy after login

PHP processing code (add_review.php):

<?php
    $item_id = $_POST['item_id'];
    $rating = $_POST['rating'];
    $comment = $_POST['comment'];

    // 将评价评论保存到数据库中
    $conn = new PDO('mysql:host=localhost;dbname=your_database_name', 'your_username', 'your_password');
    $stmt = $conn->prepare('INSERT INTO reviews (item_id, rating, comment) VALUES (?, ?, ?)');
    
    $stmt->bindParam(1, $item_id);
    $stmt->bindParam(2, $rating);
    $stmt->bindParam(3, $comment);
    
    $stmt->execute();

    // 提示用户评论已提交
    echo '您的评论已提交。谢谢!';
?>
Copy after login

In the above code, we use PDO (PHP Data Objects) to connect to the database and execute Insert operation. Note that you need to replace your_database_name, your_username and your_password with your own database information.

Finally, we can also display other users’ evaluation comments on the product details page. We query the comments table via SQL and display the results on the page.

PHP code (show_reviews.php):

<?php
    // 查询评论表
    $stmt = $conn->prepare('SELECT rating, comment FROM reviews WHERE item_id = ?');
    $stmt->bindParam(1, $item_id);
    $stmt->execute();
    $reviews = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 显示评论
    foreach ($reviews as $review) {
        echo '评分: ' . $review['rating'] . '<br>';
        echo '评论: ' . $review['comment'] . '<br><br>';
    }
?>
Copy after login

Through the above HTML form and PHP code, we successfully implemented the product evaluation and comment function for the second-hand recycling website. Users can submit ratings and comments, as well as view reviews from other users. This provides users with more comprehensive information, helping them make better purchasing decisions.

The above is the detailed content of Second-hand recycling website developed in PHP provides product evaluation and comment functions. 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!