-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupvote.php
52 lines (39 loc) · 1.18 KB
/
upvote.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
<?php
require "database.php";
session_start();
// Check to see if this user has already voted for this post
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM votes WHERE post_id = ? and username = ?");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->bind_param('ss', $_GET['post_id'], $_SESSION['username']);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
$stmt->close();
if($count == 0 and isset($_GET['post_id']) and isset($_GET['votes'])) {
// Increment the number of votes for this post by 1
$stmt = $mysqli->prepare("UPDATE posts SET votes=? WHERE post_id = ?");
$vote_num = $_GET['votes'];
$vote_num += 1;
$stmt->bind_param('ss', $vote_num, $_GET['post_id']);
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->execute();
$stmt->close();
// Insert the vote associated with this user into the table
$stmt = $mysqli->prepare("INSERT INTO votes (post_id, username) VALUES (?, ?)");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->bind_param('ss', $_GET['post_id'], $_SESSION['username']);
$stmt->execute();
$stmt->close();
}
header("Location: main.php");
exit;
?>