-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.php
101 lines (83 loc) · 2.44 KB
/
code.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
<?php
//insert.php
include("DB.php");
$form_data = json_decode(file_get_contents("php://input"));
// print_r($form_data);
$data = array();
$error = array();
if($form_data->action == "Insert")
{
if(empty($form_data->name))
{
$error["name"] = "Name is required";
}
if(empty($form_data->address))
{
$error["address"] = "Address is required";
}
if(empty($form_data->salary))
{
$error["salary"] = "Salary is required";
}
if(!empty($error))
{
$data["error"] = $error;
}
else
{
$name = mysqli_real_escape_string($connect, $form_data->name);
$address = mysqli_real_escape_string($connect, $form_data->address);
$salary = mysqli_real_escape_string($connect, $form_data->salary);
$query = "
INSERT INTO employees(name, address, salary, date) VALUES ('$name', '$address', '$salary', now())";
if(mysqli_query($connect, $query))
{
$data["message"] = "Data Inserted";
}
}
echo json_encode($data);
}
if($form_data->action == "fetch_single_data")
{
$query = "SELECT * FROM employees WHERE id='".$form_data->id."'";
$res = mysqli_query($connect, $query);
$row = mysqli_fetch_array($res);
$data['name'] = $row['name'];
$data['address'] = $row['address'];
$data['salary'] = $row['salary'];
echo json_encode($data);
}
if($form_data->action == 'Edit')
{
$name = mysqli_real_escape_string($connect, $form_data->name);
$address = mysqli_real_escape_string($connect, $form_data->address);
$salary = mysqli_real_escape_string($connect, $form_data->salary);
$query = "UPDATE employees SET name = '$name', address = '$address', salary = '$salary' WHERE id='".$form_data->id."'";
if(mysqli_query($connect, $query))
{
$data["message"] = "Data Updated";
}
echo json_encode($data);
}
if($form_data->action == 'Delete')
{
$query = "Delete from employees WHERE id='".$form_data->id."'";
if(mysqli_query($connect, $query))
{
$data["message"] = "Deleted Successfully";
}
echo json_encode($data);
}
if($form_data->action == 'Get_All'){
$query = "SELECT * FROM employees ORDER BY id";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$data[] = $row;
}
echo json_encode($data);
}
}
?>