How to make php reviews

Build trust with customers using PHP: learn how to create reviews & see a sample code example.

Creating a PHP Review System

Creating a review system in PHP is a relatively easy process. To get started, you'll need to set up your database and create a page for users to submit reviews.

Creating the Database

The first step is to create a database to store the reviews. We'll need three columns: an ID number (auto-incremented), a user-submitted review, and a rating (out of five stars).

CREATE TABLE reviews (
  id INT AUTO_INCREMENT PRIMARY KEY,
  review TEXT,
  rating INT
);

Creating the Form

Next, create a form on your website for users to submit their review. This form should include a text field for the review and a drop-down menu for the rating.

<form action="submit_review.php" method="post">
  <label for="review">Review:</label>
  <textarea name="review" rows="4" cols="50"></textarea>

  <label for="rating">Rating:</label>
  <select name="rating">
    <option value="1">1 Star</option>
    <option value="2">2 Stars</option>
    <option value="3">3 Stars</option>
    <option value="4">4 Stars</option>
    <option value="5">5 Stars</option>
  </select>

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

Submitting the Form

Once the form is submitted, you'll need to create a script to process the form data and add it to the database.

<?php
  // Connect to the database
  $db = new mysqli("localhost", "username", "password", "database");

  // Prepare the SQL query
  $stmt = $db->prepare("INSERT INTO reviews (review, rating) VALUES (?, ?)");

  // Bind the form data to the query
  $stmt->bind_param("si", $_POST["review"], $_POST["rating"]);

  // Execute the query
  $stmt->execute();
?>

Displaying the Reviews

Finally, you'll need to create a page to display the reviews. This page should query the database and output the reviews in a readable format.

<?php
  // Connect to the database
  $db = new mysqli("localhost", "username", "password", "database");

  // Query the database
  $result = $db->query("SELECT * FROM reviews ORDER BY rating DESC");

  // Loop through the results
  while ($row = $result->fetch_assoc()) {
    echo "<p><b>" . $row["rating"] . " Stars:</b> " . $row["review"] . "</p>";
  }
?>
And that's it! You now have a fully-functional review system in PHP. With a few more tweaks, you can improve the user experience and make your review system even better.

Answers (0)