1 | <?php
|
---|
2 | {
|
---|
3 | ini_set("display_errors", "On");
|
---|
4 | ini_set("mysql.trace_mode", "On");
|
---|
5 |
|
---|
6 | if (empty($_GET["fUpdCase"]))
|
---|
7 | {
|
---|
8 | echo "Please specify what kind of change you want to do.<br>";
|
---|
9 | return;
|
---|
10 | }
|
---|
11 | if (empty($_GET["fIndex"]))
|
---|
12 | {
|
---|
13 | echo "Please give an index.<br>";
|
---|
14 | return;
|
---|
15 | }
|
---|
16 | if (empty($_GET["fProgram"]))
|
---|
17 | {
|
---|
18 | echo "Please select a program.<br>";
|
---|
19 | return;
|
---|
20 | }
|
---|
21 | if (empty($_GET["fKey1"]))
|
---|
22 | {
|
---|
23 | echo "Please select a key1.<br>";
|
---|
24 | return;
|
---|
25 | }
|
---|
26 | if (empty($_GET["fKey2"]))
|
---|
27 | {
|
---|
28 | echo "Please select a key2.<br>";
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | if (empty($_GET["fValue"]))
|
---|
32 | {
|
---|
33 | echo "Please insert a value.<br>";
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | if (empty($_GET["fDescription"]))
|
---|
37 | {
|
---|
38 | echo "Please insert a description.<br>";
|
---|
39 | return;
|
---|
40 | }
|
---|
41 | if (empty($_GET["fType"]))
|
---|
42 | {
|
---|
43 | echo "Please select a type.<br>";
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | if (empty($_GET["fOriginalIndex"]))
|
---|
47 | $_GET["fOriginalIndex"]="";
|
---|
48 |
|
---|
49 | include("db.php");
|
---|
50 | $db_id = mysqli_connect($host, $user, $pw, $db);
|
---|
51 | if (mysqli_connect_errno())
|
---|
52 | {
|
---|
53 | printf("mysql_connect returned the following error: %s\n", mysqli_connect_error());
|
---|
54 | die("");
|
---|
55 | }
|
---|
56 |
|
---|
57 | //query old entry
|
---|
58 | $query1="SELECT fValue, fProgram, fKey1, fDescription, fType, fMin, fMax, fKey2 from ProgramOption WHERE fIndex='".$_GET["fIndex"]."';";
|
---|
59 | //compare new values to old values
|
---|
60 | //insert only, if at least one values is new
|
---|
61 | // update counter only if value is updated
|
---|
62 | if (!$result1=mysqli_query($db_id, $query1))
|
---|
63 | {
|
---|
64 | echo "Error sending query: ".$query1;
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | $row1 = mysqli_fetch_row($result1);
|
---|
68 |
|
---|
69 | if ($_GET["fUpdCase"]!=4
|
---|
70 | && strcmp($row1[0],$_GET["fValue"])==0
|
---|
71 | && strcmp($row1[1],$_GET["fProgram"])==0
|
---|
72 | && strcmp($row1[2],$_GET["fKey1"])==0
|
---|
73 | && strcmp($row1[7],$_GET["fKey2"])==0
|
---|
74 | && strcmp($row1[3],$_GET["fDescription"])==0
|
---|
75 | && strcmp($row1[4],$_GET["fType"])==0
|
---|
76 | //&& strcmp($row1[5],$_GET["fMin"])==0
|
---|
77 | //&& strcmp($row1[6],$_GET["fMax"])==0
|
---|
78 | )
|
---|
79 | {
|
---|
80 | // be careful, this output is evaluated by UpdateRow() in po.js
|
---|
81 | echo "no change";
|
---|
82 | mysqli_free_result($result1);
|
---|
83 | return;
|
---|
84 | }
|
---|
85 | else
|
---|
86 | mysqli_free_result($result1);
|
---|
87 |
|
---|
88 | //check if key alread exists
|
---|
89 | $query2="SELECT Count(*) from ProgramOption WHERE fKey1='".$_GET["fKey1"]."' AND fKey2='.".$_GET["fKey2"]."'";
|
---|
90 | $query2.=" AND fCounter=(Select Max(fCounter) from History)";
|
---|
91 | if (!$result2=mysqli_query($db_id, $query2))
|
---|
92 | {
|
---|
93 | echo "Error sending query: ".$query2;
|
---|
94 | return;
|
---|
95 | }
|
---|
96 | $row2 = mysqli_fetch_row($result2);
|
---|
97 | if (($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==5) && $row2[0]>0)
|
---|
98 | {
|
---|
99 | // be careful, this output is evaluated by UpdateRow() in po.js
|
---|
100 | echo "key exists";
|
---|
101 | mysqli_free_result($result2);
|
---|
102 | return;
|
---|
103 | }
|
---|
104 | else
|
---|
105 | mysqli_free_result($result2);
|
---|
106 |
|
---|
107 | //check if program alread exists
|
---|
108 | $query3="SELECT Count(*) from ProgramOption WHERE fProgram='".$_GET["fProgram"]."'";
|
---|
109 | $query3.=" AND fCounter=(Select Max(fCounter) from History)";
|
---|
110 | if (!$result3=mysqli_query($db_id, $query3))
|
---|
111 | {
|
---|
112 | echo "Error sending query: ".$query3;
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | $row3 = mysqli_fetch_row($result3);
|
---|
116 | if ($_GET["fUpdCase"]==5 && $row3[0]>0)
|
---|
117 | {
|
---|
118 | // be careful, this output is evaluated by UpdateRow() in po.js
|
---|
119 | echo "prog exists";
|
---|
120 | mysqli_free_result($result3);
|
---|
121 | return;
|
---|
122 | }
|
---|
123 | else
|
---|
124 | mysqli_free_result($result3);
|
---|
125 |
|
---|
126 | //note for fType:
|
---|
127 | //SHOW COLUMNS FROM ProgramOption LIKE 'fType';
|
---|
128 |
|
---|
129 | //new entry in table ProgramOption
|
---|
130 | //update only in case 3 if value is not changed
|
---|
131 | if ($_GET["fUpdCase"]==6)
|
---|
132 | $query0="DELETE FROM ProgramOption ";
|
---|
133 | else
|
---|
134 | {
|
---|
135 | if (strcmp($row1[0],$_GET["fValue"])==0 && $_GET["fUpdCase"]==3)
|
---|
136 | $query0="UPDATE ";
|
---|
137 | else
|
---|
138 | $query0="INSERT ";
|
---|
139 | $query0.=" ProgramOption SET ";
|
---|
140 | // set program, key, description, type
|
---|
141 | $query0.="fProgram='".$_GET["fProgram"]."'";
|
---|
142 | if (!empty($_GET["fOriginalIndex"]))
|
---|
143 | $query0.=", fOriginalIndex='".$_GET["fOriginalIndex"]."'";
|
---|
144 | $query0.=", fKey1='".$_GET["fKey1"]."'";
|
---|
145 | $query0.=", fKey2='.".$_GET["fKey2"]."'";
|
---|
146 | $query0.=", fDescription='".$_GET["fDescription"]."'";
|
---|
147 | $query0.=", fType='".$_GET["fType"]."'";
|
---|
148 | // set value
|
---|
149 | // change value in case is was
|
---|
150 | if ($_GET["fUpdCase"]==2 //inserted
|
---|
151 | || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0))// changed
|
---|
152 | $query0.=", fValue='".$_GET["fValue"]."'";
|
---|
153 | // set to NULL in case it was
|
---|
154 | if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==5 //new insert
|
---|
155 | || $_GET["fUpdCase"]==4) //delete
|
---|
156 | $query0.=", fValue=NULL";
|
---|
157 | //increase counter in case value was
|
---|
158 | if ($_GET["fUpdCase"]==2//created
|
---|
159 | || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0)//changed
|
---|
160 | || $_GET["fUpdCase"]==4)//deleted
|
---|
161 | {
|
---|
162 | $query0.=", fValidFrom=Now() ";
|
---|
163 | $query0.=", fCounter= (Select Max(fCounter) from History)+1 ";
|
---|
164 | }
|
---|
165 | else//else set current counter
|
---|
166 | $query0.=", fCounter= (Select Max(fCounter) from History) ";
|
---|
167 | }
|
---|
168 | //do update only for current row
|
---|
169 | if ((strcmp($row1[0],$_GET["fValue"])==0 && $_GET["fUpdCase"]==3) || $_GET["fUpdCase"]==6)
|
---|
170 | $query0.=" WHERE fIndex=".$_GET["fIndex"];
|
---|
171 | if ($_GET["fUpdCase"]==6)
|
---|
172 | $query0.=" AND fValidFrom='0000-00-00 00:00:00'";
|
---|
173 | $query0.=";";
|
---|
174 |
|
---|
175 | //update counter in case value was
|
---|
176 | if ($_GET["fUpdCase"]==2 //created
|
---|
177 | || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0)//changed
|
---|
178 | || $_GET["fUpdCase"]==4)//deleted
|
---|
179 | {
|
---|
180 | //update counter for other valid entries in ProgramOption
|
---|
181 | $query0.=" UPDATE ProgramOption SET ";
|
---|
182 | $query0.=" fCounter= (Select Max(fCounter) from History)+1 ";
|
---|
183 | $query0.=" WHERE fCounter= (Select Max(fCounter) from History) ";
|
---|
184 | if ($_GET["fUpdCase"]==3 || $_GET["fUpdCase"]==4)
|
---|
185 | $query0.=" AND NOT fIndex=".$_GET["fIndex"];
|
---|
186 | $query0.=" AND ((NOT ISNULL(fValue) AND NOT fValidFrom='0000-00-00 00:00:00') ";//normal entries, but not deleted ones
|
---|
187 | $query0.=" OR (ISNULL(fValue) AND fValidFrom='0000-00-00 00:00:00')); ";//new entries
|
---|
188 |
|
---|
189 | //insert new counter to history
|
---|
190 | $query0.=" INSERT History SET ";
|
---|
191 | $query0.=" fCounter= (Select Max(fCounter) from ProgramOption) ";
|
---|
192 | $query0.=", fValidFrom=Now() ";
|
---|
193 | }
|
---|
194 |
|
---|
195 | $result0=mysqli_multi_query($db_id, $query0);
|
---|
196 | if ($result0)
|
---|
197 | {
|
---|
198 | echo "Query \"" . $query0 . "\" was successful.";
|
---|
199 | return 0;
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | echo "Query \"" . $query0 . "\" was not successful.";
|
---|
204 | return 2;
|
---|
205 | }
|
---|
206 |
|
---|
207 | mysqli_free_result($result0);
|
---|
208 | mysqli_close($db_id);
|
---|
209 |
|
---|
210 | ini_set("display_errors", "Off");
|
---|
211 | ini_set("mysql.trace_mode", "Off");
|
---|
212 | }
|
---|
213 | ?>
|
---|