This repository was archived by the owner on Feb 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.php
More file actions
632 lines (581 loc) · 17.8 KB
/
query.php
File metadata and controls
632 lines (581 loc) · 17.8 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?php
define('DSN', 'mysql:host=localhost;dbname=orders');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBOPTIONS', array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
));
try {
$pdo = new PDO(DSN, DBUSER, DBPASS, DBOPTIONS);
} catch (PDOException $e) {
echo "connection failed " . $e->getMessage();
}
/*
************************************************
********** customers **********
************************************************
*/
function maxcid()
{
try {
global $pdo;
$sql = "SELECT max(id)FROM customers";
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetch()['max(id)'];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
function insertCustomers($cname, $caddress, $providence, $phone1, $phone2)
{
try {
$id = maxcid() + 1;
global $pdo;
$sql = "INSERT INTO customers(id, cname, caddress, providence, phone, phone2)
VALUES (:id, :cname, :caddress, :providence, :phone, :phone2)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id, 'cname' => $cname, 'caddress' => $caddress, 'providence' => $providence, 'phone' => $phone1, 'phone2' => $phone2]);
} catch (PDOException $e) {
echo 'c inserting failed: ' . $e->getMessage();
}
}
function customerById($id)
{
try {
global $pdo;
$sql = "SELECT * FROM customers WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetch failed ' . $e->getMessage();
}
}
function deleteCustomers($id)
{
try {
global $pdo;
$sql = "DELETE FROM customers WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
} catch (PDOException $e) {
echo 'deleting failed ' . $e->getMessage();
}
}
function updateCustomers($id, $cname, $caddress, $providence, $phone1, $phone2)
{
try {
global $pdo;
$sql = "UPDATE customers SET cname = :cname, caddress = :caddress, providence = :providence, phone = :phone, phone2 = :phone2 WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['cname' => $cname, 'caddress' => $caddress, 'providence' => $providence, 'phone' => $phone1, 'phone2' => $phone2, 'id' => $id]);
} catch (PDOException $e) {
echo 'updating failed ' . $e->getMessage();
}
}
function customerByPhone($phone)
{
try {
global $pdo;
$sql = 'SELECT * FROM customers WHERE phone = :phone OR phone2 = :phone2';
$stmt = $pdo->prepare($sql);
$stmt->bindValue('phone', $phone);
$stmt->bindValue('phone2', $phone);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function customerCount()
{
try {
global $pdo;
$sql = 'SELECT id FROM customers';
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function fetchCustomersL($slimit = 0, $limit = 100)
{
try {
global $pdo;
$sql = 'SELECT * FROM customers ORDER BY id DESC LIMIT ' . $slimit . "," . $limit;
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
function fetchCustomers()
{
try {
global $pdo;
$sql = 'SELECT * FROM customers ORDER BY cdate DESC';
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
function customerOrderCount($id)
{
try {
global $pdo;
$sql = 'SELECT id FROM orders WHERE customer_id = :id';
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'count failed ' . $e->getMessage();
}
}
/*
*************************************************
********** products ***********
*************************************************
*/
function maxpid()
{
try {
global $pdo;
$sql = "SELECT max(id) FROM products";
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetch()['max(id)'];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
function insertProduct($product_name, $price, $delivery_cost, $quantity)
{
try {
$pdisable = 1;
$id = maxpid() + 1;
global $pdo;
$sql = "INSERT INTO products(id, product_name, price, delivery_cost, quantity, pdisable)
VALUES (:id, :product_name, :price, :delivery_cost, :quantity, :pdisable)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id, 'product_name' => $product_name, 'price' => $price, 'delivery_cost' => $delivery_cost, 'quantity' => $quantity, 'pdisable' => $pdisable]);
} catch (PDOException $e) {
echo 'inserting failed ' . $e->getMessage();
}
}
function productById($id)
{
try {
global $pdo;
$sql = "SELECT * FROM products WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
return $stmt->fetch();
} catch (PDOException $e) {
echo 'fetch failed ' . $e->getMessage();
}
}
function productByName($product_name)
{
try {
global $pdo;
$sql = "SELECT * FROM products WHERE product_name = :product_name";
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_name' => $product_name]);
return $stmt->fetch();
} catch (PDOException $e) {
echo 'fetch failed ' . $e->getMessage();
}
}
function deleteProducts($id)
{
try {
global $pdo;
$sql = "DELETE FROM products WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
} catch (PDOException $e) {
echo 'deleting failed ' . $e->getMessage();
}
}
function updateProducts($id, $product_name, $price, $delivery_cost, $quantity, $pdisable)
{
try {
global $pdo;
$sql = "UPDATE products SET product_name = :product_name, price = :price, delivery_cost = :delivery_cost, quantity = :quantity, pdisable = :pdisable WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_name' => $product_name, 'price' => $price, 'delivery_cost' => $delivery_cost, 'quantity' => $quantity, 'pdisable' => $pdisable, 'id' => $id]);
} catch (PDOException $e) {
echo 'updating failed ' . $e->getMessage();
}
}
function searchProducts($product_name)
{
try {
global $pdo;
$sql = 'SELECT * FROM products WHERE product_name LIKE :product_name';
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_name' => '%' . $product_name . '%']);
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function fetchProducts()
{
try {
global $pdo;
$sql = 'SELECT * FROM products ORDER BY product_name ASC';
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
function fetchActiveProducts()
{
try {
global $pdo;
$sql = 'SELECT * FROM products WHERE pdisable = "1" ORDER BY product_name ASC';
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
/*
************************************************
********** orders **********
************************************************
*/
function maxoid()
{
try {
global $pdo;
$sql = "SELECT max(id) FROM orders";
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetch()['max(id)'];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
function insertOrder($customer_id, $discount, $notes)
{
try {
$id = maxoid() + 1;
global $pdo;
$sql = "INSERT INTO orders(id, customer_id, discount, notes)
VALUES (:id, :customer_id, :discount, :notes)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id, 'customer_id' => $customer_id, 'discount' => $discount, 'notes' => $notes]);
} catch (PDOException $e) {
echo 'inserting failed ' . $e->getMessage();
}
}
function orderById($id)
{
try {
global $pdo;
$sql = "SELECT * FROM orders WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
return $stmt->fetch();
} catch (PDOException $e) {
echo 'fetch failed ' . $e->getMessage();
}
}
function deleteOrders($id)
{
try {
global $pdo;
$sql = "DELETE FROM orders WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
} catch (PDOException $e) {
echo 'deleting failed ' . $e->getMessage();
}
}
function oprint1($id)
{
try {
global $pdo;
$sql = "UPDATE orders SET oprint = '1' WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
} catch (PDOException $e) {
echo 'updating failed ' . $e->getMessage();
}
}
function oprint0($id)
{
try {
global $pdo;
$sql = "UPDATE orders SET oprint = '0' WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
} catch (PDOException $e) {
echo 'updating failed ' . $e->getMessage();
}
}
function updateOrders($id, $customer_id, $discount, $notes)
{
try {
global $pdo;
$sql = "UPDATE orders SET customer_id = :customer_id, discount = :discount, notes = :notes WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['customer_id' => $customer_id, 'discount' => $discount, 'notes' => $notes, 'id' => $id]);
} catch (PDOException $e) {
echo 'updating failed ' . $e->getMessage();
}
}
function orderCountByCId($customer_id)
{
try {
global $pdo;
$sql = 'SELECT id FROM `orders` WHERE customer_id = :customer_id';
$stmt = $pdo->prepare($sql);
$stmt->execute(['customer_id' => $customer_id]);
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function orderByCId($customer_id, $slimit = 0, $limit = 100)
{
try {
global $pdo;
$sql = 'SELECT * FROM `orders` WHERE customer_id = :customer_id
ORDER BY odate DESC LIMIT ' . $slimit . "," . $limit;
$stmt = $pdo->prepare($sql);
$stmt->execute(['customer_id' => $customer_id]);
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function orderCountByPId($product_id)
{
try {
global $pdo;
$sql = 'SELECT id FROM `orders` WHERE `id` IN
(SELECT `order_id` FROM `order_items` WHERE `product_id` = :product_id)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_id' => $product_id]);
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function orderByPId($product_id, $slimit = 0, $limit = 100)
{
try {
global $pdo;
$sql = 'SELECT * FROM `orders` WHERE `id` IN
(SELECT `order_id` FROM `order_items` WHERE `product_id` = :product_id)
ORDER BY odate DESC LIMIT ' . $slimit . "," . $limit;
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_id' => $product_id]);
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function orderByPhone($phone, $phone2)
{
try {
global $pdo;
$sql = 'SELECT * FROM `orders` WHERE customer_id = (SELECT id FROM customers WHERE customers.phone = :phone OR customers.phone2 = :phone2)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['phone' => $phone, 'phone' => $phone2]);
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function orderCountByTime($date1, $date2)
{
try {
global $pdo;
$sql = 'SELECT id FROM orders WHERE odate >= :date1 AND odate <= :date2';
$stmt = $pdo->prepare($sql);
$stmt->execute(['date1' => $date1, 'date2' => $date2]);
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function orderCount($oprint = false)
{
$op = '';
if ($oprint === true) {
$op = ' WHERE oprint = 0';
}
try {
global $pdo;
$sql = 'SELECT id FROM orders' . $op;
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function fetchOrdersL($slimit = 0, $limit = 100)
{
try {
global $pdo;
$sql = "SELECT * FROM orders ORDER BY id DESC LIMIT " . $slimit . "," . $limit;
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
function fetchOrdersLNoPrint($slimit = 0, $limit = 100, $order = 'ASC')
{
try {
global $pdo;
$sql = 'SELECT * FROM orders WHERE oprint = "0" ORDER BY id ' . $order . ' LIMIT ' . $slimit . "," . $limit;
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
function fetchOrders()
{
try {
global $pdo;
$sql = 'SELECT * FROM orders ORDER BY id DESC';
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
/*
************************************************
********** items **********
************************************************
*/
function maxiid()
{
try {
global $pdo;
$sql = "SELECT max(id) FROM order_items";
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetch()['max(id)'];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
function insertItems($order_id, $product_id, $quantity)
{
try {
$id = maxiid() + 1;
global $pdo;
$sql = "INSERT INTO order_items (id, order_id, product_id, quantity) VALUES (:id, :order_id, :product_id, :quantity)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id, 'order_id' => $order_id, 'product_id' => $product_id, 'quantity' => $quantity]);
} catch (PDOException $e) {
echo 'fetching failed ' . $e->getMessage();
}
}
function itemsByOId($order_id)
{
try {
global $pdo;
$sql = 'SELECT * FROM order_items WHERE order_id = :order_id ';
$stmt = $pdo->prepare($sql);
$stmt->execute(['order_id' => $order_id]);
return $stmt->fetchAll();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function itemsCountByPId($product_id)
{
try {
global $pdo;
$sql = 'SELECT id FROM order_items WHERE product_id = :product_id ';
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_id' => $product_id]);
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function itemsCountByPIdDate($product_id, $date)
{
try {
global $pdo;
if (!isset($date)) {
$date = date('Y-m-d');
}
$sql = 'SELECT id FROM order_items WHERE product_id = :product_id AND id IN (SELECT id FROM orders WHERE odate = :odate)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_id' => $product_id, 'odate' => $date]);
return $stmt->rowCount();
} catch (PDOException $e) {
echo 'searching failed ' . $e->getMessage();
}
}
function pPriceByIId($id)
{
try {
global $pdo;
$sql = "SELECT products.price FROM products WHERE products.id = (SELECT order_items.product_id FROM order_items WHERE order_items.id = :id)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
return $stmt->fetch()['price'];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
function pDByIId($id)
{
try {
global $pdo;
$sql = "SELECT products.delivery_cost FROM products WHERE products.id = (SELECT order_items.product_id FROM order_items WHERE order_items.id = :id)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
return $stmt->fetch()['delivery_cost'];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
function updateItem($id, $product_id, $quantity)
{
try {
global $pdo;
$sql = "UPDATE order_items SET product_id = :product_id, quantity = :quantity WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['product_id' => $product_id, 'quantity' => $quantity, 'id' => $id]);
} catch (PDOException $e) {
echo 'updating failed ' . $e->getMessage();
}
}
function deleteItem($id)
{
try {
global $pdo;
$sql = "DELETE FROM order_items WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
} catch (PDOException $e) {
echo 'deleting failed ' . $e->getMessage();
}
}