-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_delete_meme.php
124 lines (113 loc) · 4.96 KB
/
process_delete_meme.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// things we need
session_start();
require_once("connect_to_database.php");
// check that submit is pressed
if (isset($_POST["submit"])) {
if ($_SESSION["token"] !== $_POST["token"]) {
die("Request forgery detected");
} else {
// get the id of the meme we want to delete
$memeid = (int)$_POST["memeid"];
// get number of upvotes this meme got
$statement = $mysqli->prepare("SELECT COUNT(*) FROM votes WHERE value = 2 AND memeid = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $memeid);
$statement->execute();
$statement->bind_result($numupvotes);
$statement->fetch();
$statement->close();
// get number of downvotes this meme got
$statement = $mysqli->prepare("SELECT COUNT(*) FROM votes WHERE value = 1 AND memeid = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $memeid);
$statement->execute();
$statement->bind_result($numdownvotes);
$statement->fetch();
$statement->close();
// get number of comments this meme got
$statement = $mysqli->prepare("SELECT COUNT(*) FROM meme_comments WHERE memeid = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $memeid);
$statement->execute();
$statement->bind_result($numcomments);
$statement->fetch();
$statement->close();
// get the author of this meme
$statement = $mysqli->prepare("SELECT authorid FROM memes WHERE id = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $memeid);
$statement->execute();
$statement->bind_result($authorofmeme);
$statement->fetch();
$statement->close();
// get the number of upvotes, downvotes, and comments the user currenly has
$statement = $mysqli->prepare("SELECT upvotes, downvotes, comments FROM users WHERE id = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $authorofmeme);
$statement->execute();
$statement->bind_result($userupvotes, $userdownvotes, $usercomments);
$statement->fetch();
$statement->close();
// update user's upvotes
$userupvotes = $userupvotes - $numupvotes;
$userdownvotes = $userdownvotes - $numdownvotes;
$usercomments = $usercomments - $numcomments;
// change the author of the meme's info to reflect change in
// upvotes, downvotes, and comment numbers
$statement = $mysqli->prepare("UPDATE users SET upvotes = ?, downvotes = ?, comments = ? WHERE id = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("iiii", $userupvotes, $userdownvotes, $usercomments, $authorofmeme);
$statement->execute();
$statement->close();
// delete meme votes
$statement = $mysqli->prepare("DELETE FROM votes WHERE memeid = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $memeid);
$statement->execute();
$statement->close();
// delete meme comments
$statement = $mysqli->prepare("DELETE FROM meme_comments WHERE memeid = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $memeid);
$statement->execute();
$statement->close();
// delete meme itself
$statement = $mysqli->prepare("DELETE FROM memes WHERE id = ?");
if (!$statement) {
printf("Query preparation failed: %s\n", $mysqli->error);
exit;
}
$statement->bind_param("i", $memeid);
$statement->execute();
$statement->close();
header("Location: your_memes.php");
}
} else {
header("Location: index.php");
}
?>