简体   繁体   English

如何更改我的代码,以便用户只能喜欢该帖子一次?

[英]How can I change my code so that the user can only like the post once?

I want to make a like system and this is what I have so far.我想制作一个类似的系统,这就是我目前所拥有的。 I can't figure out how to make it only increment once can anyone help me out?我不知道如何让它只增加一次有人可以帮我吗? I feel like i might have to use session variables but im not sure.我觉得我可能必须使用会话变量,但我不确定。 index.php索引.php

 <?php
    $con = mysqli_connect('localhost', 'root', '','phplikes');
    $query = "SELECT * FROM meme_vote ORDER BY vote DESC";
    $res = mysqli_query($con, $query);
    
    ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Meme Voting System</title>
   <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
</head>
<body>

<div class="container">
    <div class="row">
        <?php while($row = mysqli_fetch_assoc($res)){ $location = $row['video_location'];?>  

            <div class="col-md-4">
                <div class="post">
                    <h4 class="post-title"><?php echo $row['title']?></h4>
                    <div id="postdesign">
                        <video src="<?php echo $row['video_location'] ?>" width="100%" height="240" controls></video>
                    </div>
                    <a href="javascript:void(0)" class="btn btn-info btn-lg">
                   <span class="glyphicon glyphicon-thumbs-up " onclick="like_update('<?php echo $row['id']?>')">Vote 
                        (<span id="like_loop_<?php echo $row['id']?>"><?php echo $row['vote']?></span>) 
                    </span> 
                    
                    </a>
                </div>
            </div>
        <?php }?>    
    </div>
</div>

<script>
     function like_update(id){
        var cur_count = jQuery('#like_loop_'+id).html();
        cur_count++
        jQuery('#like_loop_'+id).html(cur_count);
        jQuery.ajax({
            url:'update_count.php',
            type:'post',
            data:'type=like&id='+id,
            success:function(result){
            }
        })
}
</script>

</body>
</html>

update_count.php update_count.php

<?php
$con = mysqli_connect('localhost', 'root', '', 'phplikes');
$type = $_POST['type'];
$id = $_POST['id'];
if($type=='like'){
    $sql = "update meme_vote set vote=vote+1 where id=$id";
     $sql2 = "update meme_vote set fake_vote=fake_vote+1 where id=$id";
}
$res = mysqli_query($con, $sql);
$res2 = mysqli_query($con, $sql2);

?> 

Also just to let you know, I already have session variables established ie.也只是为了让您知道,我已经建立了会话变量,即。 $_SESSION['user_id'] and `$_SESSION['username'] $_SESSION['user_id']和 `$_SESSION['username']

Since you already have a user_id, I'll assume you have a users table.由于您已经有一个 user_id,我假设您有一个 users 表。

You could create a new table with meme_vote_id and user_id columns, and a unique index using both fields:您可以使用 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`)
);

Then when someone votes:然后当有人投票时:

$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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码被复制并粘贴了大约 5 次,但我怎样才能让它只有一次? - My code is copy and pasted about 5 times but how can i make it so it's only once? 如何修复我的代码以仅在 javascript 中运行一次 - How can I fix my code to run only once in javascript 如何确保用户只能为每个帖子单击一次“赞”按钮? - How to ensure that a user can only click like button once for each post? 我可以设置我创建的频道权限,就像只有我的员工角色可以编辑或删除它一样。 到目前为止我的代码: - Can i set my created channels permisions like only my staff role can edit or delete it. My code so far: 我如何使用 javascript 来制作它,以便用户只能在 Lottie 动画完全播放后触发悬停事件? - How can I use javascript to make it so the user can only trigger the hover event once the Lottie animation has played in full? 如何检查某人是否喜欢过一个帖子,所以他们不能多次喜欢它? - How to check if someone has liked a post so they can't like it more than once? 我该如何创建一个共享或赞按钮,以便网站所有者可以将其放在自己的网站上,然后发布回我的网站? - How can I create a share or like button so that site owners can put it on their sites which will post back to my site? 如何更改代码以便执行? - How can I change the code so that it can execute? 我怎样才能更改我的代码,以便我不必为所有单个记录使用脚本? - How can I change my code so that I don't have to use a script for all single record? 如何为此JavaScript / HTML代码添加一个按钮,以便我的代码仅在按下按钮后才会运行? - How do i add a button to this JavaScript/HTML code, so that my code only runs once the button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM