How To Create Star Rating System Using PHP, MySQL, Ajax & jQuery

Written by
Star Rating System using php
  • 10 months ago

Star Rating System or jquery rating system help to get the feedback of customers about the product and service.By Star Rating System using php user or customer can rate the product by selecting the star, this will help the owner or service provider to improve their services as per the customer feedback.

The jquery rating system provide the range between 1 to 5 stars. The five star rating help the other customer to buy the service or product. This is commonly used in eCommerce or product based website.

In this tutorial, we will show you how to build a rating system in php

First make rating design as shown below named as index.php.

five Star Rating System using php


<?php include('config.php');?>
<!doctype html>
	<html lang="en">
		<meta charset="UTF-8">
		<meta name="viewport"
			  content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>Rating System</title>
		<link rel="stylesheet" href="" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
		<script src="" integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg=" crossorigin="anonymous"></script>
		<script src="rating.js"></script>
		<div align="center" style="background: blue; padding: 50px;color:white;">
			<i class="fa fa-star fa-2x" data-index="0"></i>
			<i class="fa fa-star fa-2x" data-index="1"></i>
			<i class="fa fa-star fa-2x" data-index="2"></i>
			<i class="fa fa-star fa-2x" data-index="3"></i>
			<i class="fa fa-star fa-2x" data-index="4"></i>
			<?php echo round($avg,2) ?>


			var ratedIndex = -1, uID = 0;
			$(document).ready(function () {
				if (localStorage.getItem('ratedIndex') != null) {
					uID = localStorage.getItem('uID');
				$('.fa-star').on('click', function () {
				   ratedIndex = parseInt($(this).data('index'));
				   localStorage.setItem('ratedIndex', ratedIndex);
				$('.fa-star').mouseover(function () {
					var currentIndex = parseInt($(this).data('index'));
				$('.fa-star').mouseleave(function () {
					if (ratedIndex != -1)
			function saveToTheDB() {
				   url: "index.php",
				   method: "POST",
				   dataType: 'json',
				   data: {
					   save: 1,
					   uID: uID,
					   ratedIndex: ratedIndex
				   }, success: function (r) {
						uID =;
						localStorage.setItem('uID', uID);
			function setStars(max) {
				for (var i=0; i <= max; i++)
					$('.fa-star:eq('+i+')').css('color', 'yellow');
			function resetStarColors() {
				$('.fa-star').css('color', 'white');

Now we create the database. Database base name is ratingsystems and table name is stars


CREATE TABLE `stars` (
  `id` int(11) NOT NULL,
  `rateIndex` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


		$conn = new mysqli('localhost', 'root', '', 'ratingsystems');
		if (isset($_POST['save'])) {
			$uID = $conn->real_escape_string($_POST['uID']);
			$ratedIndex = $conn->real_escape_string($_POST['ratedIndex']);
			if (!$uID) {
				$conn->query("INSERT INTO stars (rateIndex) VALUES ('$ratedIndex')");
				$sql = $conn->query("SELECT id FROM stars ORDER BY id DESC LIMIT 1");
				$uData = $sql->fetch_assoc();
				$uID = $uData['id'];
			} else
				$conn->query("UPDATE stars SET rateIndex='$ratedIndex' WHERE id='$uID'");
			exit(json_encode(array('id' => $uID)));
		$sql = $conn->query("SELECT id FROM stars");
		$numR = $sql->num_rows;
		$sql = $conn->query("SELECT SUM(rateIndex) AS total FROM stars");
		$rData = $sql->fetch_array();
		$total = $rData['total'];
		$avg = $total / $numR;

Conclusion: Developer Guidance Help to create Star Rating System.

Article Categories:
Codeigniter · Jquery · Laravel · Php · Wordpress

Leave a Reply

Your email address will not be published. Required fields are marked *