-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlike.php
47 lines (40 loc) · 1.67 KB
/
unlike.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
46
47
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header('Location: login.php');
}
if($_SERVER["REQUEST_METHOD"]==="POST"){
$postid=$_POST['id'];
$userid=$_SESSION['loggedin'];
include_once './classes/db.php';
try{
$pdo = dbConnection::connect();
$stmt = $pdo->prepare("SELECT * from likes WHERE username=? AND feedpostid=?");
$stmt->execute([$userid,$postid]);
$likes = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(!count($likes)){
throw new PDOException;
}
$stmt = $pdo->prepare("DELETE FROM likes WHERE username=? AND feedpostid=?");
$stmt->execute([$userid,$postid]);
$stmt = $pdo->prepare("SELECT likes from feedposts where id=?");
$stmt->execute([$postid]);
$likes = $stmt->fetchAll(PDO::FETCH_ASSOC);
$likes = $likes[0]['likes'];
$stmt = $pdo->prepare("UPDATE feedposts SET likes=? WHERE id=?");
$stmt->execute([$likes-1,$postid]);
$stmt = $pdo->prepare("SELECT count('feedpostid') from likes where feedpostid=?");
$stmt->execute([$postid]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result[0]["count('feedpostid')"]);
}catch(PDOException $e){
$stmt = $pdo->prepare("SELECT count('feedpostid') from likes where feedpostid=?");
$stmt->execute([$postid]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result[0]["count('feedpostid')"]);
// echo $e;
}
}else{
header('Location: feed.php');
}
?>