【问题标题】:Updating value of star on second click using jQuery and ajax使用 jQuery 和 ajax 在第二次点击时更新星的值
【发布时间】:2020-09-17 22:40:30
【问题描述】:

我正在研究星级评分系统,这是我项目的一部分。每当用户通过点击星星和用户生物数据进行评分时,评分就会存储在我的数据库中。现在,正如我们所知,有时,当用户对任何产品进行评分时,他/她的想法可能会瞬间改变并想要更新评分。这是我目前面临的问题。每当用户给出评分时,它就会被存储,但是当同一个用户再次给出评分时,它会再次保存该值而不是更新现有值。请帮我。我必须在 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】:

    首先你需要检查用户是否已经添加了他的评分,为此你必须根据会话用户名或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
    }
    

    希望对你有帮助,如果需要更清楚,请在下方评论

    【讨论】:

    • 忠告。不要使用rowCount。特别是不用于检查数据库中是否存在值。改为在 SQL 中执行。
    • 当然@Dharman,我会记住的,我可以知道使用rowCount 来计算值的缺点吗?
    • 非常感谢它有效。我在您的代码的帮助下解决了我的问题。实际上,我在 where 子句上犯了一些错误,就是这样。非常感谢 RohitSahu。还有一件事 rowCoint 100% 有效
    • 另外我做了一些你没有添加的更改,那只是 $stmt->execute
    • 很高兴知道@aleena
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多