Prettier
This commit is contained in:
parent
59c8b070ec
commit
33457a4548
1 changed files with 12 additions and 13 deletions
|
|
@ -4,6 +4,7 @@
|
|||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
|
||||
$input = json_decode(file_get_contents('php://input'),true);
|
||||
if (!$input) $input = array();
|
||||
|
||||
// connect to the mysql database
|
||||
$link = mysqli_connect('localhost', 'php-crud-api', 'php-crud-api', 'php-crud-api');
|
||||
|
|
@ -13,20 +14,18 @@ mysqli_set_charset($link,'utf8');
|
|||
$table = preg_replace('/[^a-z0-9_]+/i','',array_shift($request));
|
||||
$key = array_shift($request)+0;
|
||||
|
||||
if ($input) {
|
||||
// escape the columns and values from the input object
|
||||
$columns = preg_replace('/[^a-z0-9_]+/i','',array_keys($input));
|
||||
$values = array_map(function ($value) use ($link) {
|
||||
if ($value===null) return null;
|
||||
return mysqli_real_escape_string($link,(string)$value);
|
||||
},array_values($input));
|
||||
// escape the columns and values from the input object
|
||||
$columns = preg_replace('/[^a-z0-9_]+/i','',array_keys($input));
|
||||
$values = array_map(function ($value) use ($link) {
|
||||
if ($value===null) return null;
|
||||
return mysqli_real_escape_string($link,(string)$value);
|
||||
},array_values($input));
|
||||
|
||||
// build the SET part of the SQL command
|
||||
$set = '';
|
||||
for ($i=0;$i<count($columns);$i++) {
|
||||
$set.=($i>0?',':'').'`'.$columns[$i].'`=';
|
||||
$set.=($values[$i]===null?'NULL':'"'.$values[$i].'"');
|
||||
}
|
||||
// build the SET part of the SQL command
|
||||
$set = '';
|
||||
for ($i=0;$i<count($columns);$i++) {
|
||||
$set.=($i>0?',':'').'`'.$columns[$i].'`=';
|
||||
$set.=($values[$i]===null?'NULL':'"'.$values[$i].'"');
|
||||
}
|
||||
|
||||
// create SQL based on HTTP method
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue