-
Notifications
You must be signed in to change notification settings - Fork 5
/
edit-employee.php
288 lines (209 loc) · 7.67 KB
/
edit-employee.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
//connect to database
$servername = "localhost";
$username = "root";
$password = "";
$database = "php_employee_management";
//Create Connection
$connection = new mysqli($servername, $username, $password, $database);
//Check connection stablished or not!
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$id = "";
$name = "";
$email = "";
$phone = "";
$address = "";
$errorMessage = "";
$successMessage = "";
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
# Request Received using GET method
# then editing-> show the data of the employee...
//Check Id is exist or not!
if (!isset($_GET["id"])) {
#if not exist then
header("location: ./index.php");
exit;
}
//if exist employee id
$id = $_GET["id"];
$sql = "SELECT * FROM employee WHERE id=$id";
$result = $connection->query($sql);
$row = $result->fetch_assoc();
if (!$row) {
#if row does not consist any value
header("location: ./index.php");
exit;
}
//if row consist value, then read all the data from database and store it in form variable.
$name =$row["name"];
$email =$row["email"];
$phone =$row["phone"];
$address =$row["address"];
}else {
# request Reveived form POST
# POST method update the data of Employee...
$id = $_POST["id"];
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$address = $_POST["address"];
do {
# code...
if (empty($id) || empty($name) || empty($email) || empty($phone) || empty($address)) {
# code...
$errorMessage = "All fields are required!";
break;
}
$sql = "UPDATE employee SET name = '$name', email = '$email', phone = '$phone', address = '$address' WHERE id='$id'";
$result = $connection->query($sql);
if (!$result) {
die("Invalid query : " . $connection->error);
break;
}
$successMessage = "Employee Updated Successfully!";
header("location: ./index.php");
exit;
} while (false);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Management CRUD | New Employee</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css
">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<style>
/* for showing validation msg red */
*{
padding: 0px;
margin:0px;
}
.navbar{
margin: 0px;
}
.error {
color: red;
font-style: italic;
}
.mycolor{
background: #748EC6;
}
.text-w{
color:#748EC6;
}
.background-color-w{
color:#F2F5FA;
}
.section-bg {
background-color: #f2f5fa;
padding: 0px;
margin: 0px;
}
.card-shadow{
background-color: #ffffff;
border-radius: 10px;
box-shadow: 0px 5px 20px rgba(0, 0, 0, 0.1);
padding: 30px;
}
</style>
</head>
<body>
<!-- navbar start -->
<nav class="navbar navbar-expand-lg navbar-dark mycolor">
<a class="navbar-brand" href="index.php">Employee Management</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active"><a class="nav-link" href="index.php">Home
</a></li>
<li class="nav-item active"><a class="nav-link" href="./create-new-employee.php">Add
Employee</a></li>
<li class="nav-item dropdown"><a
class="nav-link dropdown-toggle" href="#" id="navbarDropdown"
role="button" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false"> Dropdown </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a> <a
class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div></li>
<li class="nav-item"><a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>
</nav>
<!-- navbar End -->
<div class="container my-5">
<h2 class="text-center mb-5">Update Employee</h2>
<!-- employee form -->
<?php
if(!empty($errorMessage)){
echo "
<div class='alert alert-warning alert-dismissible fade show' role='alert'>
<strong>$errorMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>
";
}
?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="row mb-3">
<label for="" class="col-sm-3 col-form-label">Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="name" value="<?php echo $name; ?>">
</div>
</div>
<div class="row mb-3">
<label for="" class="col-sm-3 col-form-label">Email</label>
<div class="col-sm-6">
<input type="email" class="form-control" name="email" value="<?php echo $email; ?>">
</div>
</div>
<div class="row mb-3">
<label for="" class="col-sm-3 col-form-label">Phone</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="phone" value="<?php echo $phone; ?>">
</div>
</div>
<div class="row mb-3">
<label for="" class="col-sm-3 col-form-label">Address</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="address" value="<?php echo $address; ?>">
</div>
</div>
<?php
if(!empty($successMessage)){
echo "
<div class='offset-sm-3 col-sm-6'>
<div class='alert alert-success alert-dismissible fade show' role='alert'>
<strong>$successMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>
</div>
";
}
?>
<div class="row mb-3">
<div class="offset-sm-3 col-sm-3 d-grid">
<button type="submit" class="btn btn-outline-primary">Submit</button>
</div>
<div class="col-sm-3 d-grid">
<a href="./index.php" class="btn btn-outline-primary" role="button">Cancel</a>
</div>
</div>
</form>
</div>
</body>
</html>