Home  >  Article  >  Backend Development  >  How to use PHP to implement product evaluation and review system

How to use PHP to implement product evaluation and review system

王林
王林Original
2023-05-21 15:10:561602browse

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 

<?php
$con = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8', 'myuser', 'mypassword');
$con->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 '<form method="post" action="create_comment.php">';
echo '<h3>添加评论:</h3>';
echo '<p>用户名:<input type="text" name="username" required></p>';
echo '<p>评分:<input type="number" name="rating" min="1" max="5" required></p>';
echo '<p>评论:<textarea name="comment" required></textarea></p>';
echo '<input type="hidden" name="product_id" value="'.$_GET['product_id'].'">';
echo '<input type="submit" value="提交">';
echo '</form>';

//显示评论列表
echo '<h3>最新评论:</h3>';
foreach($comments as $comment){
 echo '<hr>';
 echo '<p>用户名:'.$comment->username.'</p>';
 echo '<p>评分:'.$comment->rating.'</p>';
 echo '<p>评论:'.$comment->comment.'</p>';
 echo '<p>发表时间:'.$comment->created_at.'</p>';
}
?>

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

<?php
if($_SERVER['REQUEST_METHOD'] !== 'POST'){
 header('Location: create_comments.php');
 exit;
}

$con = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8', 'myuser', 'mypassword');
$con->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:

<a href="create_comments.php?product_id=12345">添加评论</a>

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!

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