Review system in PHP with MYSQL

Creating a review system in PHP with MySQL involves several steps, including setting up a database, creating a user interface, and implementing the logic to store and display reviews. Here's a simplified example to get you started:

1. Database Setup:

First, create a MySQL database and a table to store reviews. You can use phpMyAdmin or any other MySQL client for this. Here's a basic SQL schema for a "reviews" table:

sql code

CREATE TABLE reviews (

    id INT AUTO_INCREMENT PRIMARY KEY,

    product_id INT NOT NULL,

    user_name VARCHAR(255) NOT NULL,

    rating INT NOT NULL,

    comment TEXT,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

2. PHP and HTML for Review Form:

Create an HTML form to allow users to submit reviews. Here's a simple example of the form:

html code

<!DOCTYPE html>

<html>

<head>

    <title>Product Review</title>

</head>

<body>

    <h1>Product Review</h1>

    <form action="submit_review.php" method="POST">

        <label for="user_name">Your Name:</label>

        <input type="text" name="user_name" required><br><br>

        <label for="rating">Rating:</label>

        <input type="number" name="rating" min="1" max="5" required><br><br>

        <label for="comment">Comment:</label><br>

        <textarea name="comment" rows="4" cols="50"></textarea><br><br>

        <input type="submit" value="Submit Review">

    </form>

</body>

</html>

3. PHP for Review Submission (submit_review.php):

Create a PHP script to handle the form submission and insert the review into the database:

php code

<?php

// Include your database connection configuration here

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $product_id = 1; // Replace with your actual product ID

    $user_name = $_POST["user_name"];

    $rating = $_POST["rating"];

    $comment = $_POST["comment"];

    // Insert the review into the database

    $sql = "INSERT INTO reviews (product_id, user_name, rating, comment) VALUES (?, ?, ?, ?)";

    $stmt = $conn->prepare($sql);

    $stmt->bind_param("iss", $product_id, $user_name, $rating, $comment);

        if ($stmt->execute()) {

        echo "Review submitted successfully!";

    } else {

        echo "Error: " . $stmt->error;

    }

        $stmt->close();

    $conn->close();

}

?>

4. Displaying Reviews:

Create a PHP script to retrieve and display reviews for a product:

php code

<?php

// Include your database connection configuration here

$product_id = 1; // Replace with the actual product ID you want to display reviews for

// Retrieve reviews from the database

$sql = "SELECT user_name, rating, comment, created_at FROM reviews WHERE product_id = ?";

$stmt = $conn->prepare($sql);

$stmt->bind_param("i", $product_id);

$stmt->execute();

$result = $stmt->get_result();

// Display reviews

while ($row = $result->fetch_assoc()) {

    echo "<p><strong>{$row['user_name']} - {$row['rating']} stars</strong><br>{$row['comment']}</p>";

    echo "<small>Posted on: {$row['created_at']}</small><hr>";

}

$stmt->close();

$conn->close();

?>

Remember to replace the database connection configuration and adapt the code according to your specific requirements and security considerations. This is a basic example, and you may want to add more features such as user authentication, input validation, and error handling for a production-ready review system.

Comments

Popular posts from this blog

WORDPRESS: Content optimization and keyword research

Dependency Management: Using tools like Composer to manage dependencies in PHP projects.

Rating system in PHP with MYSQL

Caching mechanisms in MYSQL

HTML Comments: Adding comments to your HTML code