Skip to content

Commit bac66a6

Browse files
committed
Major Upgrade 👽
Major Upgrade 👽
1 parent 4247e6c commit bac66a6

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

index.php

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
if (empty($id)) {
1313
$jsonArray['error'] = true;
1414
$jsonArray['errorMessage'] = "Invalid or null value!";
15-
$_code = 403;
15+
$_code = 406;
1616
} else if (!is_numeric($id)) {
1717
$jsonArray['error'] = true;
1818
$jsonArray['errorMessage'] = "The request must be 'numeric'";
19-
$_code = 403;
19+
$_code = 406;
2020
} else {
2121
$control = $db->prepare("SELECT * FROM users WHERE id = :id");
2222
$control->bindParam(":id", $id, PDO::PARAM_INT);
@@ -31,7 +31,7 @@
3131
} else {
3232
$jsonArray['error'] = true;
3333
$jsonArray['errorMessage'] = "No value found for your request!";
34-
$_code = 403;
34+
$_code = 404;
3535
}
3636

3737

@@ -45,37 +45,38 @@
4545
$_code = 200;
4646
}
4747
} else if ($request_method === "POST") { //POST Method
48+
$userName = Security($_POST['username']);
4849
$firstName = Security($_POST['first_name']);
4950
$lastName = Security($_POST['last_name']);
50-
$age = Security($_POST['age']);
51-
$city = Security($_POST['city']);
52-
$ip = Security($_POST['ip']);
53-
if (empty($firstName) || empty($lastName) || empty($age) || empty($city) || empty($ip)) {
51+
$email = Security($_POST['email']);
52+
if (empty($userName) || empty($firstName) || empty($lastName) || empty($email)) {
5453
$jsonArray['error'] = true;
5554
$jsonArray['errorMessage'] = "Invalid or null value!";
56-
$_code = 403;
55+
$_code = 406;
5756
} else if (is_numeric($firstName) || is_numeric($lastName)) {
5857
$jsonArray['error'] = true;
5958
$jsonArray['errorMessage'] = "first or last name cannot contain numeric values.";
60-
$_code = 403;
59+
$_code = 406;
60+
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
61+
$jsonArray['error'] = true;
62+
$jsonArray['errorMessage'] = "invalid email address";
63+
$_code = 406;
6164
} else {
62-
$add = $db->prepare("INSERT INTO users (first_name, last_name, age, city, ip) VALUES (:fname, :lname, :age, :city, :ip)");
65+
$add = $db->prepare("INSERT INTO users (username, first_name, last_name, email) VALUES (:uname, :fname, :lname, :email)");
66+
$add->bindParam(":uname", $userName, PDO::PARAM_STR);
6367
$add->bindParam(":fname", $firstName, PDO::PARAM_STR);
6468
$add->bindParam(":lname", $lastName, PDO::PARAM_STR);
65-
$add->bindParam(":age", $age, PDO::PARAM_INT);
66-
$add->bindParam(":city", $city, PDO::PARAM_STR);
67-
$add->bindParam(":ip", $ip, PDO::PARAM_STR);
69+
$add->bindParam(":email", $email, PDO::PARAM_STR);
6870
$add->execute();
6971
if ($db->lastInsertId()) {
7072
$jsonArray["send_data"] = "Data sending is successful";
73+
$jsonArray['username'] = $userName;
7174
$jsonArray['first_name'] = $firstName;
7275
$jsonArray['last_name'] = $lastName;
73-
$jsonArray['age'] = $age;
74-
$jsonArray['city'] = $city;
75-
$jsonArray['ip'] = $ip;
76+
$jsonArray['email'] = $email;
7677
} else {
7778
$jsonArray['error'] = true;
78-
$_code = 404;
79+
$_code = 403;
7980
$jsonArray['errorMessage'] = "Data sending failed!";
8081
}
8182
}
@@ -84,11 +85,11 @@
8485
if (empty($id)) {
8586
$jsonArray['error'] = true;
8687
$jsonArray['errorMessage'] = "Invalid or null value!";
87-
$_code = 403;
88+
$_code = 406;
8889
} else if (!is_numeric($id)) {
8990
$jsonArray['error'] = true;
9091
$jsonArray['errorMessage'] = "The request must contain a numeric value!";
91-
$_code = 403;
92+
$_code = 406;
9293
} else {
9394
$control = $db->prepare("SELECT * FROM users WHERE id = :id");
9495
$control->bindParam(":id", $id, PDO::PARAM_INT);
@@ -104,59 +105,61 @@
104105
} else {
105106
$jsonArray['error'] = true;
106107
$jsonArray['deleteid'] = $id;
107-
$_code = 404;
108+
$_code = 403;
108109
$jsonArray['errorMessage'] = "Deletion failed.";
109110
}
110111
} else {
111112
$jsonArray['error'] = true;
112113
$jsonArray['errorMessage'] = "No value found for your request!";
113-
$_code = 403;
114+
$_code = 404;
114115
}
115116
}
116117
} else if ($request_method === "PUT") {
117118
$put_req = json_decode(file_get_contents("php://input"));
118119
$id = $put_req->id;
120+
$userName = $put_req->username;
119121
$firstName = $put_req->first_name;
120122
$lastName = $put_req->last_name;
121-
$age = $put_req->age;
122-
$city = $put_req->city;
123-
$ip = $put_req->ip;
124-
if (empty($id) || empty($firstName) || empty($lastName) || empty($age) || empty($city) || empty($ip)) {
123+
$email = $put_req->email;
124+
if (empty($id) || empty($userName) || empty($firstName) || empty($lastName) || empty($email)) {
125125
$jsonArray['error'] = true;
126126
$jsonArray['errorMessage'] = "Invalid or null value!";
127-
$_code = 403;
127+
$_code = 406;
128128
} else if (is_numeric($firstName) || is_numeric($lastName)) {
129129
$jsonArray['error'] = true;
130130
$jsonArray['errorMessage'] = "first or last name cannot contain numeric values.";
131-
$_code = 403;
131+
$_code = 406;
132+
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
133+
$jsonArray['error'] = true;
134+
$jsonArray['errorMessage'] = "invalid email address";
135+
$_code = 406;
132136
} else {
133137
$control = $db->prepare("SELECT * FROM users WHERE id = :id");
134138
$control->bindParam(":id", $id, PDO::PARAM_INT);
135139
$control->execute();
136140
$controlCount = $control->rowCount();
137141
if ($controlCount > 0) {
138-
$update = $db->prepare("UPDATE users SET first_name = :fname, last_name = :lname, age = :age, city = :city, ip = :ip WHERE id = :id");
142+
$update = $db->prepare("UPDATE users SET username = :uname, first_name = :fname, last_name = :lname, email = :email WHERE id = :id");
143+
$update->bindParam(":uname", $userName, PDO::PARAM_STR);
139144
$update->bindParam(":fname", $firstName, PDO::PARAM_STR);
140145
$update->bindParam(":lname", $lastName, PDO::PARAM_STR);
141-
$update->bindParam(":age", $age, PDO::PARAM_INT);
142-
$update->bindParam(":city", $city, PDO::PARAM_STR);
143-
$update->bindParam(":ip", $ip, PDO::PARAM_STR);
146+
$update->bindParam(":email", $email, PDO::PARAM_STR);
144147
$update->bindParam("id", $id, PDO::PARAM_INT);
145148
$update->execute();
146149
if ($update) {
147150
$jsonArray['message'] = "Update successfull.";
148151
$jsonArray['affectedId'] = $id;
149152
} else {
150153
$jsonArray['error'] = true;
151-
$_code = 404;
154+
$_code = 403;
152155
$jsonArray['errorMessage'] = "Data sending failed!";
153156
}
154157
}
155158
}
156159
} else {
157160
$jsonArray['error'] = true;
158-
$_code = 404;
159-
$jsonArray['errorMessage'] = "Invalid request or undefined method";
161+
$_code = 405;
162+
$jsonArray['errorMessage'] = "Method Not Allowed";
160163
}
161164

162165

request.rest

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,33 @@ GET http://localhost/rest-api/index.php HTTP/1.1
44

55
// Single GET Method
66

7-
GET http://localhost/rest-api/index.php?id=200 HTTP/1.1
7+
GET http://localhost/rest-api/index.php?id=101 HTTP/1.1
88

99
// POST Method
1010

1111
POST http://localhost/rest-api/index.php HTTP/1.1
1212
Content-Type: application/x-www-form-urlencoded
1313

14-
first_name=tolgahan
14+
username=tolgahan01
15+
&first_name=tolgahan
1516
&last_name=acar
16-
&age=20
17-
&city=Istanbul
18-
&ip=192.168.1.1
17+
&email=tolga@tolgahanacar.net
1918

2019
// DELETE Method
2120

22-
DELETE http://localhost/rest-api/index.php?id=200 HTTP/1.1
21+
DELETE http://localhost/rest-api/index.php?id=102 HTTP/1.1
2322

24-
// PUT Method
23+
# // PUT Method
2524

2625
PUT http://localhost/rest-api/index.php HTTP/1.1
2726
content-type: application/json
2827

2928
{
30-
"id": 199,
31-
"first_name": "Tolgahan",
32-
"last_name": "Acar",
33-
"age":21,
34-
"city":"Izmir",
35-
"ip":"192.168.1.1"
29+
"id": 101,
30+
"username": "tolgahan02",
31+
"first_name": "tolgahan",
32+
"last_name": "acar",
33+
"email": "info@tolgahanacar.net"
3634
}
3735

3836

0 commit comments

Comments
 (0)