<?php
include('admin/config.php');
$rating = (int)$_POST['rating'];
$id = (int)$_POST['id'];
if(isset($_COOKIE['rated'.$id])) {
echo"<font style=\"background:#FFF5C3;line-height:32px;font-family: Arial, sans-serif;font-size: 14px;position: relative;top: 2px;left:4px;\">Already Voted!</font>";
}else{
$query = mysql_query("SELECT * FROM newser WHERE blogid = '" . mysql_real_escape_string($id) ."'") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
if($rating > 5 || $rating < 1) {
echo"Rating can't be below 1 or more than 5";
}
setcookie("rated".$id, $id, time()+60*60*24*365);
$total_ratings = $row['total_ratings'];
$total_rating = $row['total_rating'];
$current_rating = $row['rating'];
$new_total_rating = $total_rating + $rating;
$new_total_ratings = $total_ratings + 1;
$new_rating = $new_total_rating / $new_total_ratings;
mysql_query("UPDATE newser SET total_rating = '".$new_total_rating."' WHERE blogid = '" . mysql_real_escape_string($id) ."'") or die(mysql_error());
mysql_query("UPDATE newser SET rating = '".$new_rating."' WHERE blogid = '" . mysql_real_escape_string($id) ."'") or die(mysql_error());
mysql_query("UPDATE newser SET total_ratings = '".$new_total_ratings."' WHERE blogid = '" . mysql_real_escape_string($id) ."'") or die(mysql_error());
echo"<font style=\"background:#FFF5C3;line-height:32px;font-family: Arial, sans-serif;font-size: 14px;position: relative;top: 2px;left:4px;\">Vote Recorded!</font>";
}
}
?>