How to use PHP to develop simple product review and rating functions

WBOY
Release: 2023-09-21 10:34:01
Original
1087 people have browsed it

How to use PHP to develop simple product review and rating functions

How to use PHP to develop simple product review and rating functions

As a scripting language widely used in website development, PHP can help us develop various functions Rich website. One of the common features is product reviews and ratings. This article will introduce how to use PHP to develop simple product review and rating functions, and provide specific code examples.

First, we need to create a table in the database to store comment information. The table structure is as follows:

CREATE TABLEcomments(
idint(11) NOT NULL AUTO_INCREMENT,
product_idint(11) NOT NULL,
user_idint(11) NOT NULL,
commenttext NOT NULL,
ratingint(11) NOT NULL,
created_atdatetime NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The fields in the table include: id (the unique identifier of the comment), product_id (the ID of the product being reviewed), user_id (the comment user's ID), comment (comment content), rating (score), created_at (comment creation time).

Next, we need to create a page for displaying product reviews and a page for submitting reviews.

  1. The page that displays product reviews (comments.php):

// Connect to the database, use PDO method here
$dbhost = 'localhost';
$dbname = 'your_database_name';
$username = 'your_username';
$password = 'your_password';

try {

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $username, $password);
Copy after login
Copy after login

} catch (PDOException $e) {

echo "数据库连接失败: " . $e->getMessage(); exit;
Copy after login
Copy after login

}

// Query the review list of a certain product
$product_id = $_GET['product_id'];
$stmt = $conn->prepare("SELECT * FROM comments WHERE product_id = :product_id");
$stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
$stmt ->execute();
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>


     
  • 用户:
    评论:
    评分:
    时间:
  • Copy after login

    < ;/ul>

    1. Page to submit product reviews (submit_comment.php):

    //Connect to the database
    $dbhost = 'localhost';
    $dbname = 'your_database_name';
    $username = 'your_username';
    $password = 'your_password';

    try {

    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $username, $password);
    Copy after login
    Copy after login

    } catch (PDOException $e) {

    echo "数据库连接失败: " . $e->getMessage(); exit;
    Copy after login
    Copy after login

    }

    // Get the review content and ratings from POST
    $product_id = $_POST['product_id'];
    $user_id = $_POST['user_id'];
    $comment = $_POST['comment'];
    $rating = $_POST['rating'];
    $created_at = date('Y-m-d H:i :s');

    //Insert comment data into the database
    $stmt = $conn->prepare("INSERT INTO comments (product_id, user_id, comment, rating, created_at) VALUES (:product_id , :user_id, :comment, :rating, :created_at)");
    $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
    $stmt->bindParam( ':user_id', $user_id, PDO::PARAM_INT);
    $stmt->bindParam(':comment', $comment, PDO::PARAM_STR);
    $stmt->bindParam(': rating', $rating, PDO::PARAM_INT);
    $stmt->bindParam(':created_at', $created_at, PDO::PARAM_STR);
    $stmt->execute();

    // Jump back to the product review page
    header("Location: comments.php?product_id=" . $product_id);
    exit;
    ?>

    above This is all the code examples on how to use PHP to develop simple product review and rating functions. Through these codes, we can implement a simple product review system. Users can view other users' reviews on the product page and submit their own reviews and ratings.

    Of course, this is just a simple example. In practice, it can be further improved and optimized, such as adding user authentication, comment deletion and editing functions, etc. Hope the above content can help you.

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