forked from sunshay/PHP-CRUD-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
42 lines (39 loc) · 1.67 KB
/
update.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
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: access');
header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE');
header('Content-Type: application/json; charset=UTF-8');
header('Access-Control-Allow-Headers: content-type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
header('Access-Control-Allow-Credentials: true');
// Importing DBConfig.php file.
include 'db.php';
// Getting the received JSON into $json variable.
$json = file_get_contents('php://input');
// decoding the received JSON and store into $obj variable.
$obj = json_decode($json,true);
//`id`, `firstname`, `lastname`, `age`
$id = $obj['id'];
$firstname = $obj['firstname'];
$lastname = $obj['lastname'];
$age = $obj['age'];
if (empty($firstname) || empty($lastname) || empty($age) || empty($id)) {
echo json_encode(array("message" => "Some fields are missing","status"=> "missingdata"));
}else{
// Creating SQL query and insert the record into MySQL database table.
$Sql_Query = "UPDATE crud SET firstname = '$firstname', lastname = '$lastname', age = '$age' WHERE id = '$id'";
if(mysqli_query($con,$Sql_Query)){
// If the record inserted successfully then show the message.
$response = array(
'status' => 'success', 'message' => 'Record Successfully updated Into MySQL Database.');
http_response_code(200);
echo json_encode($response);
}
else{
$response = array(
'status' => 'invalid', 'message' => 'Something is missing');
http_response_code(201);
echo json_encode($response);
}
}
mysqli_close($con);
?>