With the popularity of e-commerce, product evaluation and review systems have become one of the essential functions of shopping websites. This feature not only improves the user experience, but also provides merchants with valuable information to optimize their products and services. This article will introduce how to use PHP to implement a simple product evaluation and review system.
Step 1: Create a database table
First, we need to create a database table to store information related to evaluations and comments. We can use the following MySQL command to create a table named "comments":
CREATE TABLE comments (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
product_id INT(11) NOT NULL,
username VARCHAR(50) NOT NULL,
comment TEXT NOT NULL,
rating INT(1) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
This form Contains six columns: id (auto-incrementing primary key), product_id (product number), username (user name), comment (comment content), rating (score) and created_at (comment publication time).
Step 2: Create a PHP file
Next, we need to create a PHP file that will connect to the database and display ratings and comments. We will use PDO to establish a connection that performs the following steps:
a. Connect to DB
b. Display all comments
c. Display the form for adding comments
create_comments. php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //显示评论 if(isset($_GET['product_id'])){ $stmt = $con->prepare('SELECT * FROM comments WHERE product_id=:product_id ORDER BY created_at DESC'); $stmt->bindValue(':product_id', $_GET['product_id'], PDO::PARAM_INT); $stmt->execute(); $comments = $stmt->fetchAll(PDO::FETCH_OBJ); }else{ $stmt = $con->query('SELECT * FROM comments ORDER BY created_at DESC'); $comments = $stmt->fetchAll(PDO::FETCH_OBJ); } //显示添加评论的表格 echo ''; //显示评论列表 echo '
用户名:'.$comment->username.'
'; echo '评分:'.$comment->rating.'
'; echo '评论:'.$comment->comment.'
'; echo '发表时间:'.$comment->created_at.'
'; } ?>Step 3: Process form submission
Next, we will create another PHP file that will process the submitted form and add it to the database. The name of this file is create_comment.php. This file will:
a. Validate form data
b. Add comments to the database
c. Redirect to create_comments.php
create_comment.php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //验证表单数据 $stmt = $con->prepare('INSERT INTO comments (product_id, username, comment, rating) VALUES (:product_id, :username, :comment, :rating)'); $stmt->bindValue(':product_id', $_POST['product_id'], PDO::PARAM_INT); $stmt->bindValue(':username', $_POST['username'], PDO::PARAM_STR); $stmt->bindValue(':comment', $_POST['comment'], PDO::PARAM_STR); $stmt->bindValue(':rating', $_POST['rating'], PDO::PARAM_INT); $stmt->execute(); header('Location: create_comments.php?product_id='.$_POST['product_id']); exit; ?>
Step 4: Use the system
Now, we have created a complete product evaluation and review system. We can link the create_comments.php page with our product details page and make sure it has the product_id parameter. For example:
添加评论
Now users can use this link to access the create_comments.php page, view existing comments, and add their own comments. Merchants can improve their products and services based on user feedback.
Summary
This article briefly introduces how to use PHP to implement a basic product evaluation and review system. We created a MySQL database table called "comments" that stores information about comments and ratings. After that, we wrote two PHP files, one for displaying comments and another for adding comments. Using this system, merchants can listen to users' feedback and suggestions to continuously improve their products and services.
The above is the detailed content of How to use PHP to implement product evaluation and review system. For more information, please follow other related articles on the PHP Chinese website!