-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlike.php
50 lines (46 loc) · 1.22 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
43
44
45
46
47
48
49
50
<?php
include_once 'include/config.php';
// Connect to MySQL
$db = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
// Error checking
if (!$db) {
print "<p>Error - Could not connect to MySQL</p>";
exit;
}
$error = mysqli_connect_error();
if ($error != null) {
$output = "<p>Unable to connet to database</p>" . $error;
exit($output);
}
$insert=$_POST['insert'];
$user=$_POST['user'];
$img=$_POST['img'];
//toggle insert/delete
if ($insert==1) {
$sql = "INSERT INTO `likes` (`user_id`, `img_id`) VALUES ('$user', '$img')";
if (mysqli_query($db, $sql)) {
echo json_encode(array("statusCode"=>200));
}
else {
echo json_encode(array("statusCode"=>201));
}
}else if ($insert==2){
//use for checking if like exists
$sql = "SELECT * FROM `likes` WHERE user_id=$user AND img_id=$img";
$result=mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
echo json_encode(array("statusCode"=>200));
}
else {
echo json_encode(array("statusCode"=>201));
}
} else{
$sql = "DELETE FROM `likes` WHERE `likes`.`user_id` = '$user' AND `likes`.`img_id` = '$img'";
if (mysqli_query($db, $sql)) {
echo json_encode(array("statusCode"=>200));
}
else {
echo json_encode(array("statusCode"=>201));
}
}
?>