-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvote.php
69 lines (50 loc) · 1.64 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
<?php
include 'config.php';
try{
header("Content-Type:application/json");
$data = json_decode(file_get_contents('php://input'),true);
$id = intval($data["id"]);
$age = intval($data["age"]);
$genre = $data["genre"];
$comunity = $data["autonomous_comunity"];
$votation_id = intval($data["id_poll"]);
$answers = $data["answers"];
$conn = new mysqli($servername,$username, $pass, $dbname);
if ($votation_id === 0 || $conn->connect_error) {
echo($conn->connect_error);
throw new Exception;
}
$idvoto = "SELECT code_vote FROM Votes";
$idvotos = $conn ->query($idvoto);
$votes = array();
if ($idvotos->num_rows > 0) {
while($row = $idvotos->fetch_assoc()) {
if (count($row)!=0){
$votes[] = $row["code_vote"];
}
}
}
if (in_array($id, $votes)) {
foreach($answers as $valor){
$question = $valor['question'];
$answer = $valor['answers_question'];
$ans = "UPDATE Answers SET answer_question='$answer' WHERE id_poll='$votation_id' and question='$question' and code_vote='$id'";
$result = $conn->query($ans);
}
}else{
$sql = "INSERT INTO Votes(age,code_vote, id_poll,genre,comunity) VALUES ('$age','$id', '$votation_id','$genre','$comunity')";
$result = $conn->query($sql);
foreach($answers as $valor){
$question = $valor['question'];
$answer = $valor['answers_question'];
$ans = "INSERT INTO Answers(question,code_vote, answer_question,id_poll) VALUES ('$question','$id','$answer','$votation_id')";
$result = $conn->query($ans);
}
}
echo json_encode(array("msg"=>"1"));
$conn->close();
}catch(Exception $e){
echo json_encode(array("msg"=>0));
}
die();
?>