1 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
由于您已經(jīng)有一個(gè) user_id,我假設(shè)您有一個(gè) users 表。
您可以創(chuàng)建一個(gè)包含 meme_vote_id 和 user_id 列的新表,以及使用這兩個(gè)字段的唯一索引:
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`)
);
然后當(dāng)有人投票時(shí):
$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);
}
添加回答
舉報(bào)