-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.php
More file actions
134 lines (108 loc) · 3.67 KB
/
Copy pathdata.php
File metadata and controls
134 lines (108 loc) · 3.67 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
<?php header('Content-Type: application/json');
$write_servername = 'inventory-db-instance-1.cqpiwxt5ogq1.us-east-1.rds.amazonaws.com';
$read_servername = 'inventory-db-instance-2.cqpiwxt5ogq1.us-east-1.rds.amazonaws.com';
$username = 'master';
$password = 'lab-password';
$database = 'inventory';
$table = 'products';
// Create read connection
$read_conn = mysqli_connect($read_servername, $username, $password, $database);
// Check connection
if (!$read_conn) {
die(json_encode([
'error' => true,
'message' => 'Database read connection failed | ' . mysqli_connect_error(),
'conn' => $read_conn_arr
]));
}
// Create write connection
$write_conn = mysqli_connect($write_servername, $username, $password, $database);
// Check connection
if (!$write_conn) {
die(json_encode([
'error' => true,
'message' => 'Database write connection failed | ' . mysqli_connect_error(),
'conn' => $write_conn_arr
]));
}
// http://localhost:2001/data.php?operation=get
if ($_GET['operation'] === 'get') {
$sql = 'SELECT * from ' . $table;
$result = $read_conn->query($sql);
$products = [];
while($row = mysqli_fetch_assoc($result)) {
$products[] = $row;
}
die(json_encode([
'error' => false,
'products' => $products
]));
// http://localhost:2001/data.php?operation=add&product_name=soap&product_quantity=5&product_price=45
} elseif ($_GET['operation'] === 'add') {
if (!isset($_GET['product_name']) || !isset($_GET['product_quantity']) || !isset($_GET['product_price'])) {
die(json_encode([
'error' => true,
'message' => 'one or more missing params'
]));
}
$sql = 'INSERT into ' . $table . '(name, quantity, price) values("'. htmlentities($_GET['product_name']) . '", "' . htmlentities($_GET['product_quantity']) . '", "' . htmlentities($_GET['product_price']) . '")';
if ($write_conn->query($sql) === true) {
die(json_encode([
'error' => false,
'message' => 'Data inserted successfully'
]));
} else {
die(json_encode([
'error' => true,
'message' => mysqli_error($conn)
]));
}
// http://localhost:2001/data.php?operation=update&product_id=1&product_name=pant&product_quantity=5&product_price=45
} elseif ($_GET['operation'] === 'update') {
if (!isset($_GET['product_id']) || !isset($_GET['product_name']) || !isset($_GET['product_quantity']) || !isset($_GET['product_price'])) {
die(json_encode([
'error' => true,
'message' => 'one or more missing params'
]));
}
$sql = 'UPDATE ' . $table . ' SET name="'. htmlentities($_GET['product_name']) . '", quantity="' . htmlentities($_GET['product_quantity']) . '", price="' . htmlentities($_GET['product_price']) . '" WHERE id = ' . htmlentities($_GET['product_id']);
if ($write_conn->query($sql) === true) {
die(json_encode([
'error' => false,
'message' => 'Data updated successfully'
]));
} else {
die(json_encode([
'error' => true,
'message' => mysqli_error($write_conn)
]));
}
// http://localhost:2001/data.php?operation=delete&product_id=1
} elseif ($_GET['operation'] === 'delete') {
if (!isset($_GET['product_id'])) {
die(json_encode([
'error' => true,
'message' => 'one or more missing params'
]));
}
$sql = 'DELETE FROM ' . $table . ' WHERE id=' . htmlentities($_GET['product_id']);
if ($write_conn->query($sql) === true) {
die(json_encode([
'error' => false,
'message' => 'Data deleted successfully'
]));
} else {
die(json_encode([
'error' => true,
'message' => mysqli_error($write_conn)
]));
}
} else {
die(json_encode([
'error' => true,
'message' => 'invalid/missing operation param'
]));
}
// close connection
mysqli_close($read_conn);
mysqli_close($write_conn);