-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
100 lines (94 loc) · 3.27 KB
/
index.php
File metadata and controls
100 lines (94 loc) · 3.27 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
<?php
/*This script is written focussing on the performance rather than coding standards, readability and maintainability*/
require_once 'config.php';
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE_NAME);
$method = $_SERVER['REQUEST_METHOD'];
/*Functions*/
function buildInsertQuery($dbTable, $params){
$q = 'INSERT INTO ' . $dbTable .'(' . $params[0] . ') VALUES(' . $params[1] . ')';
echo $q;
return $q;
}
function buildUpdateQuery($dbTable, $params, $id){
global $conn;
$rs = $conn->query("SHOW INDEX FROM $dbTable WHERE Key_name = 'PRIMARY'");
$setList = array();
foreach ($params as $key => $value) {
/*If the value is a String without quotes add the quotes*/
if(is_string($value) && $value[0] != '\'' && $value[0] != '\"'){
$value = '\'' . $value . '\'';
}
$setQ = $key . '=' . $value;
array_push($setList, $setQ);
}
return 'UPDATE ' . $dbTable . ' SET ' . implode($setList, ',') . ' WHERE ' . $rs->fetch_assoc()['Column_name'] . '=' . $id;
}
function buildDeleteQuery($dbTable, $id){
global $conn;
$rs = $conn->query("SHOW INDEX FROM $dbTable WHERE Key_name = 'PRIMARY'");
return 'DELETE FROM ' . $dbTable . ' WHERE ' . $rs->fetch_assoc()['Column_name'] . '=' . $id;
}
function processParams($arr){
print_r($arr);
$keys = array();
$values = array();
foreach ($arr as $key => $value) {
array_push($keys, $key);
/*If the value is a String without quotes add the quotes*/
if(is_string($value) && $value[0] != '\'' && $value[0] != '\"'){
$value = '\'' . $value . '\'';
}
array_push($values, $value);
}
return array(implode($keys, ','), implode($values, ','));
}
function getAllTableRecords($dbTable){
global $conn;
return mysqli_fetch_all($conn->query("SELECT * FROM " . $dbTable));
}
function getTableRecord($dbTable, $id){
global $conn;
$rs = $conn->query("SHOW INDEX FROM $dbTable WHERE Key_name = 'PRIMARY'");
if(is_string($id) && $id[0] != '\'' && $id[0] != '\"'){
$id = '\'' . $id . '\'';
}
return mysqli_fetch_all($conn->query("SELECT * FROM " . $dbTable . ' WHERE ' . $rs->fetch_assoc()['Column_name'] . '=' . $id));
}
/*Processing*/
$table = $_GET['table'];
$id;
if(isset($_GET['id'])){
$id = $_GET['id'];
}
$params = '';
switch ($method) {
case 'POST':
header('Content-Type: application/json');
$params = processParams($_POST);
echo ($conn->query(buildInsertQuery($table, $params)));
break;
case 'GET':
header('Content-Type: application/json');
echo json_encode(isset($id) ? getTableRecord($table, $id) : getAllTableRecords($table));
break;
case 'PUT':
$params = processParams(json_decode(file_get_contents("php://input")), true);
$conn->query(buildUpdateQuery($table, $params, $_GET['id']));
break;
case 'DELETE':
header('Content-Type: application/json');
echo json_encode($conn->query(buildDeleteQuery($table, $_GET['id'])));
break;
case 'HEAD':
echo 'E';
break;
case 'OPTIONS':
echo 'E';
break;
default:
handle_error($request);
break;
}
echo mysqli_error($conn);
$conn->close();
?>