-
Notifications
You must be signed in to change notification settings - Fork 1
/
orders.php
executable file
·348 lines (294 loc) · 10.7 KB
/
orders.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<html lang="en">
<head>
<meta charset="utf-8">
<link href="style.css" rel="stylesheet">
<title>671BookStore</title>
<script type='text/javascript' src="scripts.js"></script>
<script>
function showUser(str) {
var user = getCookie('username');
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
showHideYears('1');
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
var from = document.getElementById('yearfrom').value;
var to = document.getElementById('yearto').value;
if(from === '')
from = '1990-01-01';
if(to === '')
to = '2100-01-01';
xmlhttp.open("GET","getOrders.php?state="+str+"&from="+from+"&to="+to,true);
xmlhttp.send();
showHideYears(str);
}
}
function ordersOnLoad()
{
showMenu();
showUser('1');
}
function showHideYears(str)
{
if(str === '4')
{
document.getElementById('yfromrow').style.display = '';
document.getElementById('ytorow').style.display = '';
document.getElementById('lookItUp').style.display = '';
}
else
{
document.getElementById('yfromrow').style.display = 'none';
document.getElementById('ytorow').style.display = 'none';
document.getElementById('lookItUp').style.display = 'none';
}
}
</script>
<script>
//update cart
function updateOrder(ISBN,OrderID,Method){
/*
var http = new XMLHttpRequest();
var url = "updateOrder.php";
var Quantity = "NA"
if(Method=="updateQuantity"){
Quantity= document.getElementById("cartQuantity"+ISBN).innerHTML;
//remove HTML tags from input
Quantity = Quantity.replace(/<[^>]*(>|$)/,"");
}
console.log("ISBN:"+ISBN + " OrderID:"+OrderID+" Method:"+Method);
var params = "ISBN="+ISBN+"&OrderID="+OrderID+"&Method="+Method+"&Quantity="+Quantity;
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
*/
console.log("ISBN:"+ISBN + " OrderID:"+OrderID+" Method:"+Method);
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("debug").innerHTML = this.responseText;
}
};
if(Method=="updateQuantity"){
var Quantity= document.getElementById("cartQuantity"+ISBN).innerHTML;
//remove HTML tags from input
var Quantity = Quantity.replace(/<[^>]*(>|$)/,"");
console.log(Quantity+" in scripts if");
xmlhttp.open("GET","updateOrder.php?ISBN="+ISBN+"&OrderID="+OrderID+"&Method="+Method+"&Quantity="+Quantity,true);
} else{
console.log(Method+"in scripts else");
xmlhttp.open("GET","updateOrder.php?ISBN="+ISBN+"&OrderID="+OrderID+"&Method="+Method,true);
}
xmlhttp.send();
}
</script>
</head>
<body onload="ordersOnLoad()">
<h1>
671 BookStore
</h1>
<div class = "menu" style="margin-bottom: 2em;">
<center>
<a href=".">Home</a>
<a href="books.php">Books</a>
<a href="search.php">Search</a>
<a id="signin" style='display:none' href="sign_in.php">Sign in</a>
<a id="register" style='display:none' href="register.php">Register</a>
<a id="myaccount" style='display:none' href="myaccount.php">My Account</a>
<a id="admin" style='display:none' href="admin.php">Admin</a>
<a id="signout" style='display:none' onclick="signout()" href="index.php">Sign Out</a>
</center>
</div>
<body>
<h1>
Your Cart
</h1>
<form>
<center>
<table style="width:50%" class="responstable">
<tr>
<center>
<select id = "order_status" name="users" onchange="showUser(this.value)">
<option value="1">Cart</option>
<option value="2">Processing</option>
<option value="3">Shipped</option>
<option value="4">Past</option>
</select>
</center>
</tr>
<form action=''>
<tr id= 'yfromrow' style='display:none'>
<th><section class='const'>Year From</section></th>
<th><input id='yearfrom' type="date" style="width:100%" name="YFrom" pattern="[0-9]{4}" title="Year should be 4 digits."></th>
</tr>
<tr id= 'ytorow' style='display:none'>
<th><section class='const'>Year To</section></th>
<th><input id='yearto' type="date" style="width:100%" name="YTo" pattern="[0-9]{4}" title="Year should be 4 digits."></th>
</tr>
<tr id='lookItUp'>
<th colspan="2"><input type="button" onclick='showUser("4")' value='Look it up'></th>
</tr>
</form>
</table>
</center>
</form>
<br>
<div id="txtHint"><b></b></div>
<div id="debug"><b></b></div>
<?php
// ----------------------------------------------------------------------------------------------------
// - Display Errors
// ----------------------------------------------------------------------------------------------------
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
// ----------------------------------------------------------------------------------------------------
// - Error Reporting
// ----------------------------------------------------------------------------------------------------
error_reporting(-1);
// ----------------------------------------------------------------------------------------------------
// - Shutdown Handler
// ----------------------------------------------------------------------------------------------------
function ShutdownHandler()
{
if(@is_array($error = @error_get_last()))
{
return(@call_user_func_array('ErrorHandler', $error));
};
return(TRUE);
};
register_shutdown_function('ShutdownHandler');
// ----------------------------------------------------------------------------------------------------
// - Error Handler
// ----------------------------------------------------------------------------------------------------
function ErrorHandler($type, $message, $file, $line)
{
$_ERRORS = Array(
0x0001 => 'E_ERROR',
0x0002 => 'E_WARNING',
0x0004 => 'E_PARSE',
0x0008 => 'E_NOTICE',
0x0010 => 'E_CORE_ERROR',
0x0020 => 'E_CORE_WARNING',
0x0040 => 'E_COMPILE_ERROR',
0x0080 => 'E_COMPILE_WARNING',
0x0100 => 'E_USER_ERROR',
0x0200 => 'E_USER_WARNING',
0x0400 => 'E_USER_NOTICE',
0x0800 => 'E_STRICT',
0x1000 => 'E_RECOVERABLE_ERROR',
0x2000 => 'E_DEPRECATED',
0x4000 => 'E_USER_DEPRECATED'
);
if(!@is_string($name = @array_search($type, @array_flip($_ERRORS))))
{
$name = 'E_UNKNOWN';
};
return(print(@sprintf("%s Error in file \xBB%s\xAB at line %d: %s\n", $name, @basename($file), $line, $message)));
};
$old_error_handler = set_error_handler("ErrorHandler");
$con=mysqli_connect("dbproject.saadmtsa.club","root","password","dbproject");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
$output = implode( ',', $output);
echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
}
$user = $_COOKIE['username'];
$orderID=null;
//add book to cart if ISBN != null/empty
if(isset($_GET['ISBN'])){
$ISBN = $_GET['ISBN'];
$sql="SELECT * FROM orders WHERE UserName = '$user' AND State='1';";
$result = mysqli_query($con,$sql);
/*debug_to_console( mysqli_error($result) );*/
/*check if there is a current cart, if not, add one*/
if (mysqli_num_rows($result)==0) {
$date = date("Y-m-d H:i:s");
$sql2 = "INSERT INTO orders (UserName, State, DateTime) VALUES ('$user', '1','$date' )";
if ($con->query($sql2) === TRUE) {
echo "New cart created successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
}
debug_to_console( $orderID );
/*get orderID*/
if($orderID==null){
$sql2 = "SELECT OrderID FROM orders WHERE State='1' AND UserName = '$user';";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$orderID = $row['OrderID'];
}
}
debug_to_console( $orderID ."line 151");
/*add book to cart*/
if($orderID!=null){
$sql2 = "INSERT INTO orderitems (OrderID, ISBN, Quantity) VALUES ('$orderID', '$ISBN','1' )";
if ($con->query($sql2) === TRUE) {
echo "New item added successfully<br>";
//update book stock
$sql = "UPDATE books SET Stock = Stock - 1 where ISBN = '$ISBN';";
if ($con->query($sql) === TRUE) {
// echo "Quantity for order updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
} else { //Book already in cart: increment Quantity
//update book stock
$sql = "UPDATE books SET Stock = Stock - 1 where ISBN = '$ISBN';";
if ($con->query($sql) === TRUE) {
//update order
$sql3 = "UPDATE orderitems SET Quantity = Quantity + 1 where OrderID = '$orderID' AND ISBN = '$ISBN';";
if ($con->query($sql3) === TRUE) {
echo "Book Stock updated successfully";
} else {
echo "Error: " . $sql3 . "<br>" . $con->error;
}
// echo "Quantity for order updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
/*
$sql3 = "UPDATE orderitems SET Quantity = Quantity + 1 where OrderID = '$orderID' AND ISBN = '$ISBN';";
if ($con->query($sql3) === TRUE) {
echo "New item updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
*/
}
}
debug_to_console( $orderID ."line 156");
mysqli_close($con);
}//close bracket for if($ISBN!="")
?>
</body>
</html>