简体   繁体   中英

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

<?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']

Since you already have a user_id, I'll assume you have a users table.

You could create a new table with meme_vote_id and user_id columns, and a unique index using both fields:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM