-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcSetUpProcess.php
More file actions
82 lines (79 loc) · 3.13 KB
/
pcSetUpProcess.php
File metadata and controls
82 lines (79 loc) · 3.13 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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'C:/xampp/htdocs/inventory/php-error.log'); // Update this path to a writable location
require 'wLInventory.php';
include $SQLOperationFile;
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');
$modeSet = $_GET['action'];
$pcSetUpObj = new pcSetUp();
// used to decode JSON objects in cyberScript.js.
// the modeSet variable is used to decided which switch expression to fall into.
// this file is associated with class pcSetUp in the SQLOp.php file.
try {
switch($modeSet) {
case 'view':
$pcSetUpObj -> connect();
$result = $pcSetUpObj ->view_table();
if (empty($result)) {
error_log(['Message: No data available']);
} else {
echo json_encode($result);
}
$pcSetUpObj ->DB_close();
break;
case 'add':
$input = json_decode(file_get_contents('php://input'), true);
$pcSetUpObj -> connect();
try {
$pcSetUpObj->add_row(
$input['pc_id'],
$input['mobo_id'],
$input['gpu_id'],
$input['ram_id'],
$input['psu_id'],
$input['monitor_id'],
$input['acc_id'],
$input['kb_id'],
$input['mouse_id'],
$input['tableLocation'],
$input['PCcondition']
);
echo json_encode(['message' => 'Row added successfully']);
} catch (Exception $e) {
echo json_encode(['error' => 'Failed to add row: ' . $e->getMessage()]);
error_log('Failed to add row: ' . $e->getMessage());
}
$pcSetUpObj ->DB_close();
break;
case 'update':
$input = json_decode(file_get_contents('php://input'), true);
$updateArray = [];
foreach($input as $inputKey => $value){
if(!empty($$value) && !$inputKey['pc_id']){
$updateArray[$inputKey] = $value;
}
}
$pcSetUpObj -> connect();
$pcSetUpObj->update_row($updateArray, $input['pc_id']);
$pcSetUpObj ->DB_close();
echo json_encode(['message' => 'Row updated successfully']);
break;
case 'delete':
$input = json_decode(file_get_contents('php://input'), true);
$pcSetUpObj -> connect();
$pcSetupObj ->delete_row($input['pc_id']);
$pcSetUpObj ->DB_close();
echo json_encode(['message' => 'Row deleted successfully']);
break;
default:
echo json_encode(['message' => 'Invalid action']);
}
} catch (Exception $e) {
error_log('Error: ' . $e->getMessage());
echo json_encode(['error' => 'Error: ' . $e->getMessage()]);
}
?>