-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
83 lines (79 loc) · 2.4 KB
/
Copy pathindex.php
File metadata and controls
83 lines (79 loc) · 2.4 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
<?php
$request = $_SERVER['REQUEST_METHOD'];
$response = array();
switch ($request) {
case 'GET':
getResponse(doGet());
break;
case 'POST':
getResponse(doPost());
break;
case 'PUT':
getResponse(doPut());
break;
case 'DELETE':
getResponse(doDelete());
break;
default:
// code...
break;
}
function doGet(){
if(@$_GET['id']){
@$id = $_GET['id'];
$where = "WHERE `s_id` = ".$id;
}else{
$id = 0;
$where = "";
}
$db_connect = mysqli_connect('localhost','root', '', 'test');
$query = mysqli_query($db_connect, "SELECT * FROM student ".$where);
while($data = mysqli_fetch_assoc($query)){
$response[] = array('Name' => $data['s_name'],'College Name' => $data['s_college_name'], 'Email ID' => $data['email'] );
}
return $response;
}
function doPost(){
if($_POST){
$db_connect = mysqli_connect('localhost','root', '', 'test');
$query = mysqli_query($db_connect, "INSERT INTO `student` (`s_name`, `s_college_name`, `email`) VALUES('".$_POST['s_name']."', '".$_POST['s_college_name']."', '".$_POST['email']."')");
if($query == true)
$response = array("message" => "Post success");
else
$response = array("message" => "Post failed");
}
return $response;
}
function doPut(){
parse_str(file_get_contents('php://input'), $_PUT);
//print_r($_PUT);
if($_PUT){
$db_connect = mysqli_connect('localhost','root', '', 'test');
$query = mysqli_query($db_connect, "UPDATE student SET
`s_name` = '".$_PUT['s_name']."',
`s_college_name` = '". $_PUT['s_college_name'] ."',
`email` = '".$_PUT['email'] ."' WHERE `s_id` = '".$_GET['id']."' ");
if($query){
$response = array('message' => " Put success");
}else {
$response = array('message' => "Put` failed");
}
}
return $response;
}
function doDelete(){
if($_GET['id']){
$db_connect = mysqli_connect('localhost','root', '', 'test');
$query = mysqli_query($db_connect, "DELETE FROM student WHERE `s_id` = '".$_GET['id']."' ");
if($query){
$response = array('message' => "Delete success");
}else {
$response = array('message' => "Delete failed");
}
}
return $response;
}
function getResponse($response){
echo json_encode(array("data" => $response));
}
?>