0

I am working on star rating system which is my part of my project. The rating is stored in my database whenever the user rates by clicking on stars along with user bio data. Now, as we know, sometimes, when user rates any product, his/her mind can change at a moment and want to update rating. This is problem which I am currently facing. Whenever the user is giving a rating, it is stored, but when the same user gives a rating again, it again saves the value instead of updating the existing value. Please help me. I have to submit my proj at 8 june.

Here is my jQuery code:

$(document).ready(function(){

 load_business_data();

 function load_business_data()
 {
  $.ajax({
   url:"m.php",
   method:"POST",
   success:function(data)
   {
    $('#business_list').html(data);
   }
  });
 }

 $(document).on('mouseenter', '.rating', function(){
  var index = $(this).data("index");
  var business_id = $(this).data('business_id');
  remove_background(business_id);
  for(var count = 1; count<=index; count++)
  {
   $('#'+business_id+'-'+count).css('color', '#ffcc00');
  }
 });

 function remove_background(business_id)
 {
  for(var count = 1; count <= 5; count++)
  {
   $('#'+business_id+'-'+count).css('color', '#ccc');
  }
 }

 $(document).on('mouseleave', '.rating', function(){
  var index = $(this).data("index");
  var business_id = $(this).data('business_id');
  var rating = $(this).data("rating");
  remove_background(business_id);
  //alert(rating);
  for(var count = 1; count<=rating; count++)
  {
   $('#'+business_id+'-'+count).css('color', '#ffcc00');
  }
 });

 $(document).on('click', '.rating', function(){
  var index = $(this).data("index");
  var business_id = $(this).data('business_id');
var username='<?php echo $_SESSION["username"];?>';
var useremail='<?php echo $_SESSION["useremail"];?>';

  $.ajax({
   url:"insert_rating.php",
   method:"POST",
   data:{index:index, business_id:business_id, username:username, useremail:useremail},
   success:function(data)
   {
if(data == 'done')
    {
       alert("There is some problem in System");
    }
    else
    {
 load_business_data();
     alert("Dear "+username+",You have rate "+index +" out of 5");

    }
   }
  });

 });

});
</script>
here is my pdo code use to insert query
<?php

//insert_rating.php
session_start();
$connect = new PDO('mysql:host=localhost;dbname=final', 'root', '');

if(isset($_POST["index"], $_POST["business_id"], $_POST["username"], $_POST["useremail"]))
{  
 $query = "
 INSERT INTO rating(business_id, rating, username,useremail) 
 VALUES (:business_id, :rating, :username, :useremail)
 ";
  $statement = $connect->prepare($query);
      $statement->execute(
  array(
   ':business_id'  => $_POST["business_id"],
   ':rating'   => $_POST["index"],
':username'   => $_POST["username"],
':useremail'   => $_POST["useremail"],
  )
);
 $result = $statement->fetchAll();
 if(isset($result))
 {
echo 'done';

?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
aleena
  • 1
  • 2

1 Answers1

-2

Firstly you need to check if the user already added his rating, to do this you have to count the rating according to the session username or id, if his rating already exists in database then you write update query else write insert query. Here's a simple demonstration for doing this:

$stmt = $connect->prepare("SELECT * FROM `rating` WHERE ''");//add your conditions here according to user and that particular rating
$ifExists = $stmt->rowCount();
if ($ifExists == 0) {
  //write your insert query here
} else {
  //write update query here
}

I hope it'll help you, if you need to be more clear, comment below

Rohit Sahu
  • 284
  • 5
  • 15
  • A word of advice. Do not use `rowCount`. Especially not for checking if a value exists in the database. Do it in SQL instead. – Dharman Jun 02 '20 at 21:20
  • sure @Dharman, I'll remember, may I know the disadvantages of using `rowCount` to count values? – Rohit Sahu Jun 03 '20 at 04:55
  • 1
    Thank you so much it works. I soved my problem with the help of your code. Actually i made some mistake on where clause that's it. Thanks alot RohitSahu. One more thing rowCoint is 100 percent working – aleena Jun 05 '20 at 14:09
  • 1
    Plus i made some more changes that you did not add and that was only $stmt->execute – aleena Jun 05 '20 at 14:11
  • glad to know @aleena – Rohit Sahu Jun 06 '20 at 07:32