【发布时间】:2015-08-10 14:56:11
【问题描述】:
我正在尝试为我的网页(Laravel 5)构建投票系统。现在,任何用户都可以多次投票,我想修复它。
这是我在 php 中的代码:
public function addVote()
{
$bikepoint = $this->bikepoint->find($id);
$userid = \Sentry::getUser()->id;
$votesid = \DB::table('bikepoints')->select('votesuid')->where('votesuid', 'LIKE' , $userid)->get();
if (...) {
$bikepoint->votes = $bikepoint->votes + 1;
$bikepoint->votesuid = $bikepoint->votesuid . $userid . '; ';
echo 'Success!';
}
else {
echo 'Fail!';
}
$bikepoint->save();
}
我的数据库表:
id title votes votesuid
1 point1 2 93; 22;
2 point2 3 92; 28; 47;
3 point3 45 ...
4 point4 32 ...
5 point5 12 ...
因此,当用户单击“添加投票”按钮时,函数会将其 ID 添加到 votesuid 字段,并将一票添加到 votes 字段。
我想通过检查他的 ID 是否放在votesuid 字段中来检查用户是否已经投票。如果是这样,用户就不能再投票了。
例如,ID 为 92、28 和 47 的用户应该无法再次投票。
【问题讨论】:
-
尤奇。不要存储以逗号分隔的投票列表。每个投票存储一行,并在 user+vote 上放置一个唯一键。
-
@ceejayoz 说了什么。如果您听从他的建议,您还可以通过在投票表上放置
unique索引来限制多票投票。 -
提前谢谢)我会重建整个投票系统)