0

我正在尝试为表格中的每个项目添加投票赞成/反对票功能。我目前正在这里实现这个:www.technabled.com/2009/02/reddit-style-voting-with-php-mysql-and.html

但是,它对我不起作用。一旦动作运行,它总是返回“失败! ”,我不知道我做错了什么了。

这是我votes.php页面内的内容。

function getAllVotes($id)
    {
    $votes = array();
    $q = "SELECT * FROM cover WHERE id='$id' ";
    $r = mysql_query($q) or die("Error: ". mysql_error(). " with query ". $q);
    if(mysql_num_rows($r)==1) {

        $row = mysql_fetch_assoc($r);
        $votes[0] = $row['votes_up'];
        $votes[1] = $row['votes_down'];
        }
    return $votes;
    }

function getEffectiveVotes($id)
    {
    /**
    Returns an integer
    **/
    $votes = getAllVotes($id);
    $effectiveVote = $votes[0] - $votes[1];
    return $effectiveVote;
    }

$id = $_POST['id'];
$action = $_POST['action'];

//get the current votes
$cur_votes = getAllVotes($id);

//ok, now update the votes

if($action=='vote_up') //voting up
{
    $votes_up = $cur_votes[0]+1;
    $q = "UPDATE cover SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down') //voting down
{
    $votes_down = $cur_votes[1]+1;
    $q = "UPDATE cover SET votes_down = $votes_down WHERE id = $id";
}

$r = mysql_query($q);
if($r) //voting done
    {
    $effectiveVote = getEffectiveVotes($id);
    echo $effectiveVote." votes";
    }
elseif(!$r) //voting failed
    {
    echo "Failed!";
    }

如果重要的话,这里是所用列的表结构:

votes_upvotes_down
类型:int(11)/空:No/默认值:0

id
类型:int(8)/空:No/额外:AUTO_INCREMENT

我使用参数进行检查:
site.com/votes.php?action=vote_up&id=123

希望有人能发现错误。感谢您的时间和帮助!

4

0 回答 0