我使用的是来自http://net.tutsplus.com/tutorials/html-css-techniques/building-a-5-star-rating-system-with-jquery-ajax-and-php/的评分脚本
但我希望它能将评分上传到数据库中,并阻止一个人一直投票给同一张图片。
这是我上传和设置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表中获取数据并将其发送给用户的方法。
发布于 2013-01-04 07:09:59
语法不正确...
$total_votes = $total_votes . +1;这会将1添加到$total_votes
$total_votes++;发布于 2013-01-04 07:19:56
这里将$value设置为与id相同。
$value = $id;在这里,您创建了一个称为变量$id的值的cookie,它创建了cookie的动态名称。
setcookie($id,$value, time()+3600*24); 要制作cookie,请始终设置静态名称
//create cookie
setcookie('widget_id',$id, time()+3600*24); // $value is useless
//read cookie
echo $_COOKIE['widget_id']; //prints the cookie
// unset cookie
unset($_COOKIE['widget_id'];);发布于 2013-01-04 09:02:34
好的,我得到了检查cookie是否设置完成的函数,所以现在我只需要从mysql表中获取数据并输出到javascript
以下是我的代码
<?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'");
if (isset($_COOKIE[$id])) {
echo 'cookie exist';
exit;
} else {
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')");
}
// Send a cookie that expires in 24 hours
setcookie($id, $id, time() + 3600 * 24);
}
?>https://stackoverflow.com/questions/14148748
复制相似问题