forked from if-itb/IF3110-2015-T1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.php
84 lines (74 loc) · 2.32 KB
/
vote.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
<?php
include 'header.php';
$conn = connectToDatabase();
$move = $_GET["move"];
$target = $_GET["target"];
$id = $_GET["id"];
if(strcmp($target,"questionVote") == 0) {
if($move == "up") {
upQuestionVote($id, $conn);
} else if($move == "down") {
downQuestionVote($id, $conn);
}
} else {
if($move == "up") {
upAnswerVote($id, $conn);
} else if($move == "down") {
downAnswerVote($id, $conn);
}
}
function upQuestionVote($id, $conn) {
$sql = "Update question Set question_vote=question_vote+1 Where question_id=".$id;
if (mysqli_query($conn, $sql)) {
$sql = "Select question_vote From question Where question_id=".$id;
$row = mysqli_fetch_assoc(mysqli_query($conn, $sql));
echo $row["question_vote"];
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
function downQuestionVote($id, $conn) {
$sql = "Select question_vote From question Where question_id=".$id;
$row = mysqli_fetch_assoc(mysqli_query($conn, $sql));
if($row["question_vote"] <= 0) {
echo $row["question_vote"];
} else {
$sql = "Update question Set question_vote=question_vote-1 Where question_id=".$id;
if (mysqli_query($conn, $sql)) {
$sql = "Select question_vote From question Where question_id=".$id;
$row = mysqli_fetch_assoc(mysqli_query($conn, $sql));
echo $row["question_vote"];
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
function upAnswerVote($id, $conn) {
$sql = "Update answer Set answer_vote=answer_vote+1 Where answer_id=".$id;
if (mysqli_query($conn, $sql)) {
$sql = "Select answer_vote From answer Where answer_id=".$id;
$row = mysqli_fetch_assoc(mysqli_query($conn, $sql));
echo $row["answer_vote"];
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
function downAnswerVote($id, $conn) {
$sql = "Select answer_vote From answer Where answer_id=".$id;
$row = mysqli_fetch_assoc(mysqli_query($conn, $sql));
if($row["answer_vote"] <= 0) {
echo $row["answer_vote"];
} else {
$sql = "Update answer Set answer_vote=answer_vote-1 Where answer_id=".$id;
if (mysqli_query($conn, $sql)) {
$sql = "Select answer_vote From answer Where answer_id=".$id;
$row = mysqli_fetch_assoc(mysqli_query($conn, $sql));
echo $row["answer_vote"];
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
include 'footer.php';
closeDatabase($conn);
?>