Home  >  Article  >  Backend Development  >  How to use PHP to develop a simple product review function

How to use PHP to develop a simple product review function

WBOY
WBOYOriginal
2023-09-21 08:49:41906browse

How to use PHP to develop a simple product review function

How to use PHP to develop a simple product review function

With the rise of e-commerce, the product review function has become an indispensable function to facilitate the interaction between users communication and consumers’ evaluation of products. This article will introduce how to use PHP to develop a simple product review function, and attach specific code examples.

  1. Create database

First, we need to create a database to store product review information. Create a database named "product_comments" and create a table named "comments" in it. The table structure is as follows:

CREATE TABLE comments (

id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
username VARCHAR(50),
comment TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

  1. Connect to the database

In the PHP code, we need to connect to the database. Create a file named "config.php" with the following content:

$host = 'localhost';
$dbname = 'product_comments';
$username = 'root';
$password = 'password';

$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

Please make sure to replace $host, $dbname, $username and $password with yours own database information.

  1. Display comments

In the product details page, we need to display the review information of the product. Create a file called "product.php" and add the following code in it:

include 'config.php';

$product_id = $_GET ['product_id'];

$stmt = $conn->prepare('SELECT * FROM comments WHERE product_id = :product_id');
$stmt->bindParam(':product_id', $product_id);
$stmt->execute();
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($comments as $comment) {

echo '<p>' . $comment['username'] . '于' . $comment['created_at'] . '发表评论:<br>' . $comment['comment'] . '</p>';

}
?>

Please note that in the above code, we obtain the ID of the product through the GET method, then query the review information of the product from the database, and add it Displayed on the product detail page.

  1. Add a comment

In order to add a comment, we need to add a comment form to the product details page. Add the following code in the "product.php" file:

<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="text" name="username" placeholder="用户名">
<textarea name="comment" placeholder="评论"></textarea>
<input type="submit" value="提交评论">

  1. Handling comment submissions

Create a file called "add_comment.php" and add the following code:

include ' config.php';

$product_id = $_POST['product_id'];
$username = $_POST['username'];
$comment = $_POST['comment'];

$stmt = $conn->prepare('INSERT INTO comments (product_id, username, comment) VALUES (:product_id, :username, :comment)');
$stmt->bindParam (':product_id', $product_id);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':comment', $comment);
$stmt->execute();

header('Location: product.php?product_id=' . $product_id);
?>

In the above code, We obtain the submitted comment information through the POST method and insert it into the database. Then use the header function to redirect back to the product details page and display the comments just added.

The above are the steps and code examples for developing a simple product review function using PHP. You can make appropriate modifications and extensions according to your own needs to implement more complex functions, such as paging display of comments, user login, etc. Hope it helps your development!

The above is the detailed content of How to use PHP to develop a simple product review 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