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';
?>