-
Notifications
You must be signed in to change notification settings - Fork 0
/
like.php
42 lines (35 loc) · 1.5 KB
/
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
<?php
include 'includes/db.php';
if(isset($_POST['username']) && isset($_POST['post_id']) && isset($_POST['like'])){
$username = $_POST['username'];
$post_id = $_POST['post_id'];
$query = "SELECT * FROM `likes` WHERE like_by= :username";
$result = $conn->prepare($query);
$result->execute(['username'=>$username]);
$count = $result->rowCount();
if($count < 1){
$query = "INSERT INTO `likes` (`like_id`, `post_id`, `like_by`) VALUES (NULL, :post_id, :username)";
$result = $conn->prepare($query);
$result->execute(['username'=>$username,'post_id'=>$post_id]);
$query1 = "UPDATE post set post_like = post_like + 1 WHERE post_id =:post_id";
$result1 = $conn->prepare($query1);
$result1->execute(['post_id'=>$post_id]);
}
}
if(isset($_POST['username']) && isset($_POST['post_id']) && isset($_POST['unlike'])){
$username = $_POST['username'];
$post_id = $_POST['post_id'];
$query = "SELECT * FROM `likes` WHERE like_by= :username";
$result = $conn->prepare($query);
$result->execute(['username'=>$username]);
$count = $result->rowCount();
if($count == 1){
$query = "DELETE FROM `likes` WHERE like_by= :username";
$result = $conn->prepare($query);
$result->execute(['username'=>$username]);
$query = "UPDATE post set post_like = post_like - 1 WHERE post_id =:post_id";
$result = $conn->prepare($query);
$result->execute(['post_id'=>$post_id]);
}
}
?>