I have a button that, when clicked, adds 1 to the value of the table in my database (for example, to the button).
When the user clicks the button:
var button_id = $(e.target).closest('div').attr('id');
var id = button_id.substring(12);
$.ajax({
method: 'POST',
url: 'update_like.php',
data: {id:id},
success: function(){
},
error: function(){
}
})
update_like.php
$id = mysqli_real_escape_string($conn,$_POST['id']);
if (!empty($_POST)){
mysqli_query($conn,"UPDATE posts SET likes=likes+1 WHERE id='$id'");
}
Q: How can he check if you liked it so that they can only resemble a message once?
I just thought of something like this:
Enter a column called "likers" or something similar, with a long list of user IDs who liked the post.
i.e.
5 217 16 31893 ... User IDs
In update_like.php, check if the user who registered the identifier in this line is not found. Sort of:
$id = mysqli_real_escape_string($conn,$_POST['id']);
$result = mysqli_query($conn,"SELECT likers FROM posts WHERE id='$id'");
$likers = mysqli_fetch_assoc($result);
$user_id = $_SESSION['id']; // user ID
if (!empty($_POST)){
if (!(preg_match("/\b$user_id\b/", $likers))){
mysqli_query($conn,"UPDATE posts SET likes=likes+1 WHERE id='$id'");
}
}
Is it possible?
source
share