我正在尝试为表格中的每个项目添加投票/投票功能。我目前正在努力实现这一个:www.technabled.com/2009/02/reddit-style-voting-with-php-mysql-and.html
但是,它对我不起作用。一旦行动,它总是返回“ FAILED!”,我无法弄清楚我做错了什么。
这就是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_up 和 votes_down :
键入:int(11)
/ Null:No
/默认:0
列 id :
输入:int(8)
/空:No
/额外:AUTO_INCREMENT
我使用参数进行检查:
的 site.com/votes.php ?action=vote_up&id=123
希望有人能发现错误。感谢您的时间和帮助!