-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncQuestions.php
86 lines (80 loc) · 2.03 KB
/
syncQuestions.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
<?php
require 'dbConnect.php';
$userID;
if(isset($_GET['id']))
$userID = (int)$_GET['id'];
else
{
header("Status: 412 Precondition Failed");
die("Error getting user id");
}
$con = makeSQLI();
if($con === false)
{
header("Status: 412 Precondition Failed");
die("Error connecting to sql");
}
$count = 0;
if(isset($_POST['questions']) && $_POST['questions'] != "")
{
$newUpdateQuestionsStringArray = explode("\n", $_POST['questions']);
foreach ($newUpdateQuestionsStringArray as $newQuestionString)
{
if($newQuestionString != "")
{
list($qID, $question, $type, $positive) = explode("|", $newQuestionString);
if($qID == -1)
{
// insert new
$sql = "INSERT INTO tb_user_questions (`fk_user_id`, `question` ,`type` ,`positive`)
VALUES (?, ?, ?, ?)";
if(!($stmt = $con->prepare($sql)))
{
header("Status: 412 Precondition Failed");
die("Error prep'ing new question");
}
$stmt->bind_param('isss', $userID, $question, $type, $positive);
if(!$stmt->execute())
{
header("Status: 412 Precondition Failed");
die("Error adding question");
}
}
else
{
// update on row num
$cleanQ = $con->escape_string($question);
$sql = "UPDATE tb_user_questions SET question = '". $cleanQ ."'
, type = '".$type."', positive = '". $positive ."'
WHERE fk_user_id = " . $userID .
" AND id = " . $qID;
if(!$result = $con->query($sql))
{
header("Status: 412 Precondition Failed");
die("Error updating question");
}
}
}
}
}
$cleanID = $con->escape_string($userID);
$sql = "SELECT * FROM tb_user_questions WHERE fk_user_id = '$cleanID'";
if($result = $con->query($sql))
{
$toPrint = "";
while ($row = $result->fetch_object()) {
$toPrint .= $row->id."|".$row->question."|".$row->type."|".$row->positive."|".$row->added."\n";
$count++;
}
}
if($count == 0)
{
header("Status: 204 No Content");
echo "None";
}
else
{
header("Status: 202 Accepted");
echo $toPrint;
}
?>