Browse Source

updated for PHP7 tests

Maurits van der Schee 7 years ago
parent
commit
e6b92de0c6
1 changed files with 24 additions and 22 deletions
  1. 24
    22
      extras/core.php

+ 24
- 22
extras/core.php View File

1
 <?php
1
 <?php
2
- 
2
+
3
 // get the HTTP method, path and body of the request
3
 // get the HTTP method, path and body of the request
4
 $method = $_SERVER['REQUEST_METHOD'];
4
 $method = $_SERVER['REQUEST_METHOD'];
5
 $request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
5
 $request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
6
 $input = json_decode(file_get_contents('php://input'),true);
6
 $input = json_decode(file_get_contents('php://input'),true);
7
- 
7
+
8
 // connect to the mysql database
8
 // connect to the mysql database
9
-$link = mysqli_connect('localhost', 'user', 'pass', 'dbname');
9
+$link = mysqli_connect('localhost', 'php-crud-api', 'php-crud-api', 'php-crud-api');
10
 mysqli_set_charset($link,'utf8');
10
 mysqli_set_charset($link,'utf8');
11
- 
11
+
12
 // retrieve the table and key from the path
12
 // retrieve the table and key from the path
13
 $table = preg_replace('/[^a-z0-9_]+/i','',array_shift($request));
13
 $table = preg_replace('/[^a-z0-9_]+/i','',array_shift($request));
14
 $key = array_shift($request)+0;
14
 $key = array_shift($request)+0;
15
- 
16
-// escape the columns and values from the input object
17
-$columns = preg_replace('/[^a-z0-9_]+/i','',array_keys($input));
18
-$values = array_map(function ($value) use ($link) {
19
-  if ($value===null) return null;
20
-  return mysqli_real_escape_string($link,(string)$value);
21
-},array_values($input));
22
- 
23
-// build the SET part of the SQL command
24
-$set = '';
25
-for ($i=0;$i<count($columns);$i++) {
26
-  $set.=($i>0?',':'').'`'.$columns[$i].'`=';
27
-  $set.=($values[$i]===null?'NULL':'"'.$values[$i].'"');
15
+
16
+if ($input) {
17
+  // escape the columns and values from the input object
18
+  $columns = preg_replace('/[^a-z0-9_]+/i','',array_keys($input));
19
+  $values = array_map(function ($value) use ($link) {
20
+    if ($value===null) return null;
21
+    return mysqli_real_escape_string($link,(string)$value);
22
+  },array_values($input));
23
+
24
+  // build the SET part of the SQL command
25
+  $set = '';
26
+  for ($i=0;$i<count($columns);$i++) {
27
+    $set.=($i>0?',':'').'`'.$columns[$i].'`=';
28
+    $set.=($values[$i]===null?'NULL':'"'.$values[$i].'"');
29
+  }
28
 }
30
 }
29
- 
31
+
30
 // create SQL based on HTTP method
32
 // create SQL based on HTTP method
31
 switch ($method) {
33
 switch ($method) {
32
   case 'GET':
34
   case 'GET':
38
   case 'DELETE':
40
   case 'DELETE':
39
     $sql = "delete `$table` where id=$key"; break;
41
     $sql = "delete `$table` where id=$key"; break;
40
 }
42
 }
41
- 
43
+
42
 // execute SQL statement
44
 // execute SQL statement
43
 $result = mysqli_query($link,$sql);
45
 $result = mysqli_query($link,$sql);
44
- 
46
+
45
 // die if SQL statement failed
47
 // die if SQL statement failed
46
 if (!$result) {
48
 if (!$result) {
47
   http_response_code(404);
49
   http_response_code(404);
48
   die(mysqli_error());
50
   die(mysqli_error());
49
 }
51
 }
50
- 
52
+
51
 // print results, insert id or affected row count
53
 // print results, insert id or affected row count
52
 if ($method == 'GET') {
54
 if ($method == 'GET') {
53
   if (!$key) echo '[';
55
   if (!$key) echo '[';
60
 } else {
62
 } else {
61
   echo mysqli_affected_rows($link);
63
   echo mysqli_affected_rows($link);
62
 }
64
 }
63
- 
65
+
64
 // close mysql connection
66
 // close mysql connection
65
 mysqli_close($link);
67
 mysqli_close($link);

Loading…
Cancel
Save