我有一个投票系统,允许用户对项目进行upvote和downvote。我希望改进系统,因为用户可以多次投票。下面你可以看到我的代码。
<h4> Votes </h4>
<table>
<tr>
<th>Up-Votes</th>
<th>Down-Votes</th>
<tr/>
<tr>
<?php
mysql_connect("localhost", "root", "PASS") or die(mysql_error());
mysql_select_db("DBNAME") or die(mysql_error());
if ( isset( $_POST['Upvote'] ) )
{
mysql_query ("UPDATE reports SET up = up + 1 WHERE reportID = $id");
} else if (isset( $_POST['Downvote'] ))
{
mysql_query ("UPDATE reports SET down = down + 1 WHERE reportID = $id");
}
$data = mysql_query("SELECT * FROM reports WHERE reportID = $id") or die(mysql_error());
while($ratings = mysql_fetch_array( $data ))
{
Echo "<td>" .$ratings['up']."</td>";
Echo "<td>" .$ratings['down']."</td>";
}
?>
</tr>
</table>
<br/>
<form method='POST'>
<input type='submit' value='up' name='Upvote' class="myButton">
<input type='submit' value='down' name='Downvote' class="myButton">
</form>
是否有人有任何可以防止重复投票的建议?
我的系统上可能存在数千个项目,我不确定如何让用户免于对特定报告进行投票
干杯
答案 0 :(得分:1)
建立投票数据库:
CREATE TABLE Votes (
Value int,
User int
);
Value
对于upvotes为1,对于downvotes为-1,User
为用户ID。只需在投票时覆盖现有列:
if (mysqli_fetch_array(mysqli_query($con, "SELECT FROM `Votes` WHERE `User` = ".$theuserid)) {
mysqli_query("UPDATE `Votes` SET `Value` = ".$mynewvalue." WHERE `User` = ".$theuserid);
} else {
mysqli_query("INSERT INTO `Votes`(`User`,`Value`) VALUES (".$theuserid.",".$mynewvalue.")");
}
要获得投票总数,请使用:
SELECT sum(Value) FROM Votes
答案 1 :(得分:0)
然后在你的php
中$voted = $this->getUserVoted(escape($_POST['user']));
if($voted) { $this->addError($language_votes['you_already_rated']); return; }