但我希望它将评分上传到数据库并阻止一个人一直投票给同一张照片。
这是我上传和设置 cookie 的脚本:
<?php
// Get id and voted value
$id = $_POST['widget_id'];
preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
$vote = $match[1];
// Connect to database and find the row which have the id
$get = mysql_query("SELECT * FROM ratings WHERE id = '$id'");
while ($getdata = mysql_fetch_array($get)) {
$total_votes = $getdata['total_votes'];
$total_value = $getdata['total_value'];
// See if the votes and value is 0 and if it aint it update the database and if it is were creating a new row
if ($total_votes != 0 && $total_value != 0) {
$total_votes++;
mysql_query("UPDATE ratings SET total_votes = '$total_votes' WHERE
id = '$id'");
} else {
mysql_query("INSERT INTO ratings (id, total_votes, total_value)
VALUES ('$id', '1', '$vote')");
}
// Sets the cookie
$value = $id;
// Send a cookie that expires in 24 hours
setcookie($id, $value, time() + 3600 * 24);
?>
但是如果用户已经投票了,他仍然可以投票,所以我需要某种方式来检查他是否有 cookie 以及从 mysql 表中获取数据并将其发送给用户的方法。