dongxuan1314 2017-04-09 10:50
浏览 14
已采纳

如何确保用户针对特定帖子仅点击我的投票链接一次?

I have a mysql tables...

'users' with uname(a string) as the primary key. 'posts' with id as the primary key.

'posts' has a columnm- 'votes'

When a user is logged in, he can vote posts. The problem is how do i stop him from voting more than once?

Link:(Note: $q is id of the post)

<a href='./vote.php?upid=".$q."'>Upvote</a>

PHP:

if(isset($_GET["upid"])) {
        $plus = $_GET["upid"];
        $query = "UPDATE posts SET votes = votes + 1 WHERE id='$plus'";
        $result = mysqli_query($dbc, $query) or die('Error querying database!');
    }
  • 写回答

2条回答 默认 最新

  • dsdvr06648 2017-04-09 10:56
    关注

    Take two steps:

    1. Onclick set the link's style to display:none. or hide() with jQuery. (UX friendly) (PHP: on future page loads, do not present the link if a vote is already cast by the user.)

    2. In your database, save the action in a new/separate table that only allows INSERT only if unique combination of userid and postid. Then you can leverage that table to count the total votes for the post id.

    p.s. By storing individual votes using unique post id and user id, you can also allow a user to undo their vote if you wish.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64