0

So far, I am trying to limit the user from liking a certain post more than once. The problem is that when I click "like" on a certain post, the like count will go up every time I click the link, even though it should be limited to just one.

This is the user.php file:

echo "<a href='likes.php?id=$row[0]'>$row[6]</a>";

The "id=$row[0]" indicates to the id_post column in the database. "$row[6]" is the column in the database which shows the like count.

Here is the likes.php file:

<?php
include 'db.php';
connect();

$id = $_GET['id'];

$sql1 = "SELECT * FROM posts";
$result1 = mysqli_query($link, $sql1) or die(mysqli_error($link));
$row = mysqli_fetch_row($result);


if ($row[6] == 0) {
    $sql2 = "UPDATE posts SET likes = likes + 1 WHERE id_post = '$id'";
    $result2 = mysqli_query($link, $sql2) or die(mysqli_error($link));
} 

if ($row[6] == 1) {
    exit(header("Location: user.php"));
}

header("Location: user.php");
?>

What's the problem with my code?

AlienHunter 67
  • 55
  • 2
  • 3
  • 6
  • Acquire the user's ip address and only allow one like per distinct IP address per post id, see https://stackoverflow.com/questions/6794782/definitive-way-to-get-user-ip-address-php – clearshot66 May 22 '17 at 19:47
  • Nothing in that code records who voted, so you will have no way to limit voting. You'll need a votes/likes table that holds an identifier for the user voting and what post they are voting on and only add an entry to it if the user hasn't already voted on that content. – Eric Hodges May 22 '17 at 20:06

1 Answers1

0
SELECT * FROM posts

Your query is selecting all rows from the posts table, and not filtering based on the ID in your $id variable.

Try changing your query to:

SELECT * FROM posts WHERE COLUMN_NAME = '$id'

This way $row[6] will refer to the correct ID in your posts table.

Nick Dawes
  • 2,089
  • 1
  • 13
  • 20