-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
105 lines (69 loc) · 2.73 KB
/
Copy pathindex.php
File metadata and controls
105 lines (69 loc) · 2.73 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
101
102
103
104
105
<?php
header('Content-Type: application/json');
/***************************************
$data = array('key' => 'value');
header('Content-Type: application/json');
echo json_encode($data);
***************** */
header('Access-Control-Allow-Origin: *');
/***************************************
* header('Access-Control-Allow-Origin: https://example.com');
***************** */
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');
header('Access-Control-Allow-Headers: Content-Type, X-Custom-Header,Access-Control-Allow-Header,X-Requested-With, Authorization');
`require 'connection.php';
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
$sql = "SELECT *FROM user ";
$result = $conn->query($sql);
$uses = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$uses[] = $row;
}
}
echo json_encode($uses);
break;
case 'POST':
$data = json_decode(file_get_contents('php://input'), true);
$name = $data['name'];
$email = $data['email'];
$password = $data['password'];
$sql = "INSERT INTO user (name , email, password) VALUES ('$name','$email','$password')";
if ($conn->query($sql) === TRUE) {
echo json_encode(['Message' => 'user successfully inserted']);
}
break;
case 'PUT':
$data = json_decode(file_get_contents('php://input'), true);
$user_id = $data['user_id'];
$name = $data['name'];
$email = $data['email'];
$password = $data['password'];
$sql = "UPDATE user SET name ='$name' , email ='$email', password ='$password' WHERE id='$user_id'";
if ($conn->query($sql) === TRUE) {
echo json_encode(['Message' => 'User data successfully updated']);
} else {
echo json_encode(['error' => 'User data does not updated' . $conn->error]);
}
break;
case 'DELETE':
$data = json_decode(file_get_contents('php://input'), true);
$user_id = $data['user_id'];
$name = $data['name'];
$email = $data['email'];
$password = $data['password'];
$sql = "DELETE FROM user WHERE id='$user_id'";
if ($conn->query($sql) === TRUE) {
echo json_encode(['Message' => 'User data deleted']);
} else {
echo json_encode(['error' => 'User data does not deleted' . $conn->error]);
}
break;
default:
http_response_code(405);
echo json_encode(['error' => 'User data does not deleted' . $conn->error]);
break;
}
?>