评级系统协助

时间:2013-01-03 23:07:01

标签: php jquery mysql ajax

我正在使用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表中获取数据并将其发送给用户的方法。

4 个答案:

答案 0 :(得分:2)

语法不正确......

$total_votes = $total_votes . +1;

这会将1添加到$total_votes

$total_votes++;

答案 1 :(得分:1)

在这里,您将$ 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'];);

答案 2 :(得分:0)

好的,我有功能来检查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); 
}
?>

答案 3 :(得分:-1)

您不应该跟踪用户是否已使用Cookie对特定内容进行投票。

相反,您应该检查您的数据库,以确保用户在提交INSERT投票查询之前没有对该特定内容进行投票。

如果用户已经投票,可能会执行UPDATE而不是INSERT将其投票更改为新值。

所以你会这样检查:

SELECT * FROM ratings
WHERE userid=:userid

您将受益于使用PDO(http://php.net/manual/en/book.pdo.php)来绑定必要的参数,然后可以轻松检查返回的行数(http://php.net/manual/en/pdostatement.rowcount.php)。

如果返回1行,则用户已经投票。然后,您可以使用新的投票值更新投票。

如果返回0行,则表示用户尚未投票。然后你可以像在上面的代码中一样进行插入。

如果返回的行数超过1行,则已经插入了多个投票,您需要删除所有投票中的所有投票。

我建议为各个页面/对象添加一列,以便您可以使用此功能。

相关问题