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

我想制作一个类似的系统,这就是我到目前为止所拥有的。我不知道如何让它只增加一次,有人可以帮助我吗?我觉得我可能必须使用会话变量,但我不确定。索引.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>

<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>


另外只是为了让您知道,我已经建立了会话变量,即。$_SESSION['user_id']和 `$_SESSION['用户名']


不负相思意
浏览 129回答 1
1回答

BIG阳

由于您已经有一个 user_id,我假设您有一个 users 表。您可以创建一个包含 meme_vote_id 和 user_id 列的新表,以及使用这两个字段的唯一索引:CREATE TABLE `meme_voters` (&nbsp; `meme_vote_id` INT UNSIGNED NOT NULL,&nbsp; `user_id` INT UNSIGNED NOT NULL,&nbsp; 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 ) {&nbsp; &nbsp; $sql = "INSERT INTO meme_voters (meme_vote_id, user_id) VALUES ({$id}, {$_SESSION['user_id']})";&nbsp; &nbsp; mysqli_query($con, $sql);&nbsp; &nbsp; $sql = "UPDATE meme_vote SET vote=vote+1 WHERE id=$id";&nbsp; &nbsp; $res = mysqli_query($con, $sql);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript