1 回答
TA贡献1859条经验 获得超6个赞
由于您已经有一个 user_id,我假设您有一个 users 表。
您可以创建一个包含 meme_vote_id 和 user_id 列的新表,以及使用这两个字段的唯一索引:
CREATE TABLE `meme_voters` (
`meme_vote_id` INT UNSIGNED NOT NULL,
`user_id` INT UNSIGNED NOT NULL,
UNIQUE INDEX `unique_voter` (`meme_vote_id`, `user_id`)
);
然后当有人投票时:
$sql = mysqli_query($con,"SELECT COUNT(*) AS count FROM meme_voters WHERE meme_vote_id={$id} AND user_id={$_SESSION['user_id']}" );
$rs = mysqli_fetch_object($sql);
if ( $rs->count == 0 ) {
$sql = "INSERT INTO meme_voters (meme_vote_id, user_id) VALUES ({$id}, {$_SESSION['user_id']})";
mysqli_query($con, $sql);
$sql = "UPDATE meme_vote SET vote=vote+1 WHERE id=$id";
$res = mysqli_query($con, $sql);
}
添加回答
举报