Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_application
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
arron.hovingham
php_application
Commits
f1b21118
Commit
f1b21118
authored
May 21, 2018
by
arron.hovingham
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
0 deletions
+55
-0
api.php
api.php
+55
-0
No files found.
api.php
0 → 100644
View file @
f1b21118
<?php
// get the HTTP
$method
=
$_SERVER
[
'REQUEST_METHOD'
];
$input
=
json_decode
(
file_get_contents
(
'php://input'
),
true
);
$request
=
explode
(
'/'
,
trim
(
$_SERVER
[
'PATH_INFO'
],
'/'
));
// Connecting to the database
$link
=
mysqli_connect
(
'localhost'
,
'arron.hovingham'
,
'RU9TYBDH'
,
'application_data'
);
mysqli_set_charset
(
$link
,
'utf8'
);
// retrieve the table and the key
$table
=
preg_replace
(
'/[^a-z0-9_]+/i'
,
''
,
array_shift
(
$request
));
$key
=
array_shift
(
$request
)
+
0
;
// 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
));
// 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
]
.
'"'
);}
// HTTP Methods become SQL based
switch
(
$method
)
{
case
'GET'
:
$sql
=
"select * from `
$table
`"
.
(
$key
?
" WHERE id=
$key
"
:
''
);
break
;
case
'PUT'
:
$sql
=
"update `
$table
` set
$set
where id=
$key
"
;
break
;
case
'POST'
:
$sql
=
"insert into `
$table
` set
$set
"
;
break
;
case
'DELETE'
:
$sql
=
"delete `
$table
` where id=
$key
"
;
break
;
}
// excecute or kill the SQL
$result
=
mysqli_query
(
$link
,
$sql
);
if
(
!
$result
)
{
http_response_code
(
404
);
die
(
mysqli_error
());}
// print the results
if
(
$method
==
'GET'
)
{
if
(
!
$key
)
echo
'['
;
for
(
$i
=
0
;
$i
<
mysqli_num_rows
(
$result
);
$i
++
)
{
echo
(
$i
>
0
?
','
:
''
)
.
json_encode
(
mysqli_fetch_object
(
$result
));}
if
(
!
$key
)
echo
']'
;}
elseif
(
$method
==
'POST'
)
{
echo
mysqli_insert_id
(
$link
);}
else
{
echo
mysqli_affected_rows
(
$link
);}
mysqli_close
(
$link
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment