-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.php
146 lines (126 loc) · 3.47 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
require 'database.php';
if(isset($_POST['save_student']))
{
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$course = mysqli_real_escape_string($con, $_POST['course']);
if($name == NULL || $email == NULL || $phone == NULL || $course == NULL)
{
$res = [
'status' => 422,
'message' => 'All fields are mandatory'
];
echo json_encode($res);
return;
}
$query = "INSERT INTO tbl_students (name,email,phone,course) VALUES ('$name','$email','$phone','$course')";
$query_run = mysqli_query($con, $query);
if($query_run)
{
$res = [
'status' => 200,
'message' => 'Student Created Successfully'
];
echo json_encode($res);
return;
}
else
{
$res = [
'status' => 500,
'message' => 'Student Not Created'
];
echo json_encode($res);
return;
}
}
if(isset($_POST['update_student']))
{
$student_id = mysqli_real_escape_string($con, $_POST['student_id']);
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$course = mysqli_real_escape_string($con, $_POST['course']);
if($name == NULL || $email == NULL || $phone == NULL || $course == NULL)
{
$res = [
'status' => 422,
'message' => 'All fields are mandatory'
];
echo json_encode($res);
return;
}
$query = "UPDATE tbl_students SET name='$name', email='$email', phone='$phone', course='$course'
WHERE id='$student_id'";
$query_run = mysqli_query($con, $query);
if($query_run)
{
$res = [
'status' => 200,
'message' => 'Student Updated Successfully'
];
echo json_encode($res);
return;
}
else
{
$res = [
'status' => 500,
'message' => 'Student Not Updated'
];
echo json_encode($res);
return;
}
}
if(isset($_GET['student_id']))
{
$student_id = mysqli_real_escape_string($con, $_GET['student_id']);
$query = "SELECT * FROM tbl_students WHERE id='$student_id'";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run) == 1)
{
$student = mysqli_fetch_array($query_run);
$res = [
'status' => 200,
'message' => 'Student Fetch Successfully by id',
'data' => $student
];
echo json_encode($res);
return;
}
else
{
$res = [
'status' => 404,
'message' => 'Student Id Not Found'
];
echo json_encode($res);
return;
}
}
if(isset($_POST['delete_student']))
{
$student_id = mysqli_real_escape_string($con, $_POST['student_id']);
$query = "DELETE FROM tbl_students WHERE id='$student_id'";
$query_run = mysqli_query($con, $query);
if($query_run)
{
$res = [
'status' => 200,
'message' => 'Student Deleted Successfully'
];
echo json_encode($res);
return;
}
else
{
$res = [
'status' => 500,
'message' => 'Student Not Deleted'
];
echo json_encode($res);
return;
}
}