使用jQuery和Ajax在第二次单击时更新星星的值

问题描述 投票:0回答:1

我正在研究星级系统,这是我项目的一部分。每当用户通过单击星标以及用户生物数据进行评分时,该评分就会存储在我的数据库中。现在,我们知道,有时,当用户对任何产品进行评分时,他/她的想法可能会立即改变,并希望更新评分。这是我目前面临的问题。每当用户给出评分时,都会存储该评分,但是当同一用户再次给出评分时,它将再次保存该值,而不是更新现有值。请帮我。我必须在6月8日提交项目。

这是我的jQuery代码:

$(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';

?>
php jquery ajax pdo
1个回答
0
投票

首先,您需要检查用户是否已经添加了他的评分,为此,您必须根据会话用户名或ID对评分进行计数,如果他的评分已存在于数据库中,则您编写更新查询,否则编写插入查询。这是执行此操作的简单演示:

$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
}

我希望它能对您有所帮助,如果您需要更清楚的说明,请在下面评论

© www.soinside.com 2019 - 2024. All rights reserved.