-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-comment-like.php
45 lines (34 loc) · 1.78 KB
/
update-comment-like.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
header('Content-Type: application/json');
include 'db.php';
$userID = mysqli_real_escape_string($conn, $_POST['userID']); //get user id from request
//check for POST request and required parameters
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['commentID'])) {
$commentID = mysqli_real_escape_string($conn, $_POST['commentID']);
//check if the user has already liked the comment
$checkLike = "SELECT * FROM CommentLikes WHERE CommentID = '$commentID' AND UserID = '$userID'";
$checkResult = mysqli_query($conn, $checkLike);
if (mysqli_num_rows($checkResult) > 0) {
//user has liked the comment before, so unlike it
$deleteLike = "DELETE FROM CommentLikes WHERE CommentID = '$commentID' AND UserID = '$userID'";
mysqli_query($conn, $deleteLike);
//decrement likes count in Comments table
$decrementLikes = "UPDATE Comments SET Likes = Likes - 1 WHERE CommentID = '$commentID'";
mysqli_query($conn, $decrementLikes);
$response = ['success' => true, 'message' => 'Comment unliked successfully.'];
} else {
//user hasn't liked the comment before, so like it
$addLike = "INSERT INTO CommentLikes (CommentID, UserID) VALUES ('$commentID', '$userID')";
mysqli_query($conn, $addLike);
//increment Likes count in Comments table
$incrementLikes = "UPDATE Comments SET Likes = Likes + 1 WHERE CommentID = '$commentID'";
mysqli_query($conn, $incrementLikes);
$response = ['success' => true, 'message' => 'Comment liked successfully.'];
}
echo json_encode($response);
} else {
echo json_encode(['success' => false, 'message' => 'Invalid request or missing comment ID.']);
}
//close db connection
$conn->close();
?>