-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertQuestions.php
74 lines (71 loc) · 2.45 KB
/
insertQuestions.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
<?php
if(isset($_POST['sendForm']) && $_SESSION['formInsert']){
//get id of form inserted
$stmt = $currentUser->getCon()->prepare("SELECT * FROM form_list ORDER BY id DESC LIMIT 1");
$stmt->execute();
$getId = $stmt->get_result();
// print_r($_POST);
//if form exists and only one of this type present
if(mysqli_num_rows($getId) == 1){
$row = $getId->fetch_assoc();
$id = $row['id'];
//store question and its attributes in db
foreach ($_POST as $key => $value) {
if(strpos($key,"answer") === 0 && isset($_POST[$key])){
if($value != ''){
//set required attribute of question to false (default)
$required = "false";
//get answer type from question name
$trim = str_replace("answer","",$key);
$ansType = preg_replace('/[0-9]+/',"",$trim);
//whether required or not
$checkedName = preg_replace('/[^0-9]+/', "", $trim);
if(isset($_POST['required'.$checkedName])){
$required = "true";
}
//insert question into db
$stmt = $currentUser->getCon()->prepare("INSERT INTO form_questions (form_id,question,answer_type,required) VALUES (?, ?, ?, ?)");
$stmt->bind_param('isss',$id,$value,$ansType,$required);
$stmt->execute();
$stmt->close();
//get options and insert into options table
if($ansType == "mcq" || $ansType == "dropdown"){
$options = getoptions($checkedName,$ansType);
$stmt = $currentUser->getCon()->prepare("SELECT id from form_questions ORDER BY id DESC LIMIT 1");
$stmt->execute();
$row = $stmt->get_result();
$row = $row->fetch_assoc();
$qid = $row['id'];
foreach ($options as $key => $value) {
$stmt = $currentUser->getCon()->prepare("INSERT INTO form_options (question_id,options) VALUES (?, ?)");
$stmt->bind_param('is',$qid,$value);
$stmt->execute();
$stmt->close();
}
}
}
else{
echo '<script>alert("Enter values for all questions")</script>';
header("location: formCreate.php");
}
}
}
$_SESSION['questionInsert'] = true;
}
else{
die("duplicate forms exist");
}
}
function getoptions($name,$ansType){
$questionNum = substr($name,0,-1) ? substr($name,0,-1) : $name;
$options = array();
// echo $questionNum;
foreach ($_POST as $key => $value) {
if(strpos($key, "answer".$ansType.$questionNum) === 0 && $key != "answer".$ansType.$questionNum){
array_push($options, $value);
unset($_POST[$key]);
}
}
return $options;
}
?>