Is there anyway to prevent a user from clicking like multiple times without having them be inserted into the database like this?
I know I could just disable the button, but I want do this all without reloading the page.
$dbh = mysqli_connect("localhost", "root", "", "testdb");
$postid = $_POST['postid'];
if(isset($_POST['liked'])&& empty($_SESSION[$postid])){
$_SESSION[$postid] = TRUE;
$userid = $_SESSION['username'];
$result = mysqli_query($dbh,"SELECT * FROM user_images WHERE id=$postid");
$row = mysqli_fetch_array($result);
$n = $row['likes'];
mysqli_query($dbh,"INSERT INTO likes(username, postid) VALUES('$userid', '$postid')");
mysqli_query($dbh,"UPDATE user_images SET likes=$n+1 WHERE id=$postid");
echo $n+1;
exit();
}
if (isset($_POST['unliked'])){
$postid = $_POST['postid'];
$result = mysqli_query($dbh,"SELECT * FROM user_images WHERE id=$postid");
$row = mysqli_fetch_array($result);
$n = $row['likes'];
$userid = $_SESSION['username'];
//delete from likes before updating
mysqli_query($dbh,"DELETE FROM likes WHERE postid=$postid AND username=$userid");
mysqli_query($dbh,"UPDATE user_images SET likes=$n-1 WHERE id=$postid");
echo $n-1;
exit();
}