-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
59 lines (50 loc) · 1.84 KB
/
api.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
<?php
$host = 'localhost';
$dbname = 'test';
$username = 'root';
$password = '';
$mysqli = new mysqli($host, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$action = $_GET['action'] ?? '';
$table = $_GET['table'] ?? '';
switch ($action) {
case 'get_data':
$result = $mysqli->query("SELECT * FROM $table ORDER BY id DESC");
$data = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($data);
break;
case 'add_data':
$data = json_decode(file_get_contents('php://input'), true);
$columns = implode(", ", array_keys($data));
$values = implode("', '", array_map([$mysqli, 'real_escape_string'], array_values($data)));
$query = "INSERT INTO $table ($columns) VALUES ('$values')";
$mysqli->query($query);
echo json_encode(['status' => 'success']);
break;
case 'update_data':
$data = json_decode(file_get_contents('php://input'), true);
$id = $mysqli->real_escape_string($data['id']);
unset($data['id']);
$setClause = '';
foreach ($data as $key => $value) {
$setClause .= "`$key` = '" . $mysqli->real_escape_string($value) . "', ";
}
$setClause = rtrim($setClause, ', ');
$query = "UPDATE $table SET $setClause WHERE id = $id";
$mysqli->query($query);
echo json_encode(['status' => 'success']);
break;
case 'delete_data':
$data = json_decode(file_get_contents('php://input'), true);
$id = $mysqli->real_escape_string($data['id']);
$query = "DELETE FROM $table WHERE id = $id";
$mysqli->query($query);
echo json_encode(['status' => 'success']);
break;
default:
echo json_encode(['error' => 'Invalid action']);
break;
}
$mysqli->close();