-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
109 lines (75 loc) · 2.46 KB
/
index.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
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
106
107
108
109
<?php
require_once('./connect.php');
header('content-type:application/json');
$action_name = $_POST["actionName"];
if($action_name == "getUsers"){
$data = [];
$search = isset($_POST["search"]) ? $_POST["search"] : '';
if(empty($search))
$query = "SELECT * FROM user";
else
$query = "SELECT * FROM user where name like '%$search%'";
$result = mysqli_query($con, $query);
$rowCount = mysqli_num_rows($result);
if($rowCount > 0){
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
}
$message = "success";
} else {
$message = "no data found.";
}
$result_data = array('status' => 1, 'message' => $message, 'data' => $data);
echo json_encode($result_data);
}
if($action_name == "insertUser"){
$name = isset($_POST["name"]) ? $_POST["name"] : '';
$age = isset($_POST["age"]) ? $_POST["age"] : '';
$username = isset($_POST["username"]) ? $_POST["username"] : '';
$password = isset($_POST["password"]) ? $_POST["password"] : '';
$query = "INSERT INTO user(name, age, username, password) VALUES('$name', '$age', '$username', '$password')";
$result = mysqli_query($con, $query);
if($result){
$status = 1;
$message = "User added successfuly";
} else {
$status = 0;
$message = "Unable to add user";
}
$result_data = array('status' => $status, 'message' => $message);
echo json_encode($result_data);
}
if($action_name == "updateUser"){
$id = $_POST['id'];
$name = isset($_POST["name"]) ? $_POST["name"] : '';
$age = isset($_POST["age"]) ? $_POST["age"] : '';
$username = isset($_POST["username"]) ? $_POST["username"] : '';
$password = isset($_POST["password"]) ? $_POST["password"] : '';
$query = "UPDATE user SET name='$name', age='$age', username='$username', password='$password' WHERE id=$id";
$result = mysqli_query($con, $query);
if($result){
$status = 1;
$message = "User updated successfuly";
} else {
$status = 0;
$message = "Unable to update user";
}
$result_data = array('status' => $status, 'message' => $message);
echo json_encode($result_data);
}
if($action_name == "deleteUser"){
$id = $_POST['id'];
$query = "DELETE FROM user WHERE id=$id";
$result = mysqli_query($con, $query);
echo $result;
if($result){
$status = 1;
$message = "User deleted successfuly";
} else {
$status = 0;
$message = "Unable to delete user";
}
$result_data = array('status' => $status, 'message' => $message);
echo json_encode($result_data);
}
?>