PHP - 我希望用户不可能多次投票

PHP - I want the user to have no possibility to upvote more than one time

我正在与 PHP 和 MySQL 一起开发一个小型社交网络 我希望用户有可能投票 post,我已经这样做了,但问题是我希望用户只能投票一次。

这是我的 PHP 代码:

if (isset($_POST['heart']))
{ 
  $identificateur = $_POST['hide'];
  $Q = "UPDATE posts  SET avis = avis + 1 where id=$identificateur ";
  $bdd->query($Q);
}

这是点赞按钮的形式:

<form action="p.php" method="POST">
        <label><?php 
        if ($avis != 0)
                    {
                        echo $avis; 
                    }
        ?></label>
        <input type="hidden" value="<?php echo "$id" ?>" name="hide">
        <input type="submit" value=" " id="heart" name="heart"> 
</form>

每个用户只应有权点击一次投票图标。 非常感谢您。

您需要第二个 table,它有两个字段,post_idvoter_id。 (post_voter).post_id 有一个包含 post table 的 id 的外键,voter_id 有一个指向用户 table 的外键。此 table 指定用户投票给 post 和 post 的选民。当用户尝试给 post 投票时,您必须检查 post_voter table 并获取 post_id 等于 post 的 id 的行数voter_id 等于用户的 id。如果 count = 0,这意味着用户还没有对 post 投票,他现在可以投票了。否则,用户已经对 post 点赞了,他不能再点赞了。