-
Notifications
You must be signed in to change notification settings - Fork 0
/
addrecipe.php
147 lines (129 loc) · 2.91 KB
/
addrecipe.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
function connect_to_server(){
$mysqli = new mysqli(
"oniddb.cws.oregonstate.edu",
"cecilma-db","WWRm6SQCx2MUU1cl",
"cecilma-db"
);
if(!$mysqli || $msqli->connect_errno){
send_error($mysqli->connect_errno . ": " . $mysqli->connect_error);
}
return $mysqli;
}
function send_error($error){
$returnval = array(
"data" => false,
"success" => false
);
if ($error){
$returnval["error"] = $error;
}
echo json_encode($returnval);
exit();
}
function get_ingredients(){
$iarray = $_POST["ingredients"];
//combine ingredients into string
if (count($iarray) == 1){
$istring = $iarray[0];
}else if(count($iarray) == 2){
$istring = $iarray[0] . " and " . $iarray[1];
}else{
$istring = $iarray[0];
for ($i = 1; $i < count($iarray); $i++){
if ($i == count($iarray) - 1){
$istring = $istring . ", and " . $iarray[$i];
}else{
$istring = $istring . ", " . $iarray[$i];
}
}
}
return $istring;
}
function get_recipe_items(){
//flavor
if (array_key_exists("flavor", $_POST)){
if ($_POST["flavor"] == "other"){
if (array_key_exists("otherflavor", $_POST) && $_POST["otherflavor"] != ""){
$flavor = $_POST["otherflavor"];
}else{
send_error("wrong.");
}
}else{
$flavor = $_POST["flavor"];
}
}
//nutrition
if (array_key_exists("nut", $_POST)){
if ($_POST["nut"] == "true"){
$nut = 1;
}else{
$nut = 0;
}
}
//price
if (array_key_exists("cheap", $_POST)){
if ($_POST["cheap"] == "true"){
$cheap = 1;
}else{
$cheap = 0;
}
}
//ingredients
if (array_key_exists("ingredients", $_POST)){
$istring = get_ingredients();
}
$rec_items = array(
"flavor" => $flavor,
"nut" => $nut,
"cheap" => $cheap,
"ingredients" => $istring
);
return $rec_items;
}
function check_values($x){
if (!array_key_exists("flavor", $x) || !$x["flavor"]){
return 0;
}else if ($x["flavor"] == "other" && !$x["otherflavor"]){
//they said "other" but did not specify
return 0;
}
if (!array_key_exists("nut", $x) || !$x["nut"]){
return 0;
}
if (!array_key_exists("cheap", $x) || !$x["flavor"]){
return 0;
}
if (!array_key_exists("ingredients", $x) || !$x["ingredients"]){
return 0;
}
return 1;
}
function enter_recipe($mysqli, $x){
$statement = $mysqli->prepare(
"INSERT INTO ramen_recipes(flavor, ingredients, nut, cheap)VALUES (?, ?, ?, ?)"
);
$statement->bind_param("ssss", $x["flavor"], $x["ingredients"], $x["nut"], $x["cheap"]);
$statement->execute();
$statement->close();
}
//main
$mysqli = connect_to_server();
if ($_POST){
$rec_items = get_recipe_items();
//check to make sure everything is there
if (check_values($rec_items)){
enter_recipe($mysqli, $rec_items);
$returnval = array(
"success" => true
);
echo json_encode($returnval);
}else{
send_error("Invalid values. Please fill in the form correctly.");
}
// foreach ($rec_items as $key => $value) {
// echo $key . ": " . $value . "<br>";
// }
}
mysqli_close($mysqli);
?>