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
|
<?php
declare(strict_types=1);
error_reporting(E_ALL);
function connect_to_database() {
$dbh = new mysqli('localhost', 'root', '');
$dbh->set_charset('utf8mb4');
$dbh->query('CREATE DATABASE IF NOT EXISTS EMPLOYEE_DB');
$dbh->select_db('EMPLOYEE_DB');
$dbh->query('CREATE TABLE IF NOT EXISTS `Employee` (`EID` INT PRIMARY KEY AUTO_INCREMENT, `Ename` VARCHAR(255), `Address` TEXT, `Phno` VARCHAR(20), `Salary` DECIMAL(10,2), `Category` ENUM("GEN", "SC", "ST", "OBC"), `Language` VARCHAR(255))');
return $dbh;
}
function html_prologue($title) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $title; ?></title>
<style>
body { font-family: sans-serif; }
table { width: 80%; margin: 20px auto; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<?php
}
function display_success() {
html_prologue('Operation successful');
?>
<h2>Insertion successful.</h2>
<?php
}
function display_failure($reason) {
html_prologue('Operation failure');
?>
<h2>Operation failed</h2>
<p>Reason: <?php echo $reason; ?></p>
<?php
die();
}
function show_employee_form() {
html_prologue('Employee Form');
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h2>Employee Form</h2>
<label for="ename">Name:</label><br>
<input type="text" id="ename" name="ename" required><br>
<label for="address">Address:</label><br>
<textarea id="address" name="address" required></textarea><br>
<label for="phno">Phone Number:</label><br>
<input type="tel" id="phno" name="phno" required><br>
<label for="salary">Salary:</label><br>
<input type="text" id="salary" name="salary" required><br>
<label for="category">Category:</label><br>
<input type="radio" id="gen" name="category" value="GEN" checked>
<label for="gen">GEN</label>
<input type="radio" id="sc" name="category" value="SC">
<label for="sc">SC</label>
<input type="radio" id="st" name="category" value="ST">
<label for="st">ST</label>
<input type="radio" id="obc" name="category" value="OBC">
<label for="obc">OBC</label><br>
<label for="language">Language:</label><br>
<input type="checkbox" id="bengali" name="language[]" value="Bengali">
<label for="bengali">Bengali</label>
<input type="checkbox" id="english" name="language[]" value="English">
<label for="english">English</label>
<input type="checkbox" id="hindi" name="language[]" value="Hindi">
<label for="hindi">Hindi</label><br><br>
<input type="submit" value="Submit">
</form>
<?php
}
function insert_employee_record($dbh) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$ename = $_POST['ename'];
$address = $_POST['address'];
$phno = $_POST['phno'];
$salary = $_POST['salary'];
$category = $_POST['category'];
$languages = implode(", ", $_POST['language']);
$stmt = $dbh->prepare('INSERT INTO `Employee` (`Ename`, `Address`, `Phno`, `Salary`, `Category`, `Language`) VALUES (?, ?, ?, ?, ?, ?)');
$stmt->bind_param('sssdss', $ename, $address, $phno, $salary, $category, $languages);
$stmt->execute();
$stmt->close();
display_success();
}
}
function display_employee_records($dbh) {
$order_by = isset($_POST['order_by']) ? $_POST['order_by'] : 'Ename';
$desc_order = isset($_POST['desc_order']);
$field_labels = [
'EID' => 'Employee ID',
'Ename' => 'Name',
'Address' => 'Address',
'Phno' => 'Phone Number',
'Salary' => 'Salary',
'Category' => 'Category',
'Language' => 'Language'
];
$allowed_fields = array_keys($field_labels);
if (!in_array($order_by, $allowed_fields))
display_failure('Invalid order_by field.');
$order_clause = $order_by . ($desc_order ? ' DESC' : '');
$result = $dbh->query('SELECT * FROM `Employee` ORDER BY ' . $order_clause);
html_prologue('Employee Records');
?>
<h2>Employee Records</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="order_by">Order By:</label>
<select name="order_by" id="order_by"><?php
foreach ($field_labels as $field => $label) {
echo '<option value="' . $field . '"';
if ($order_by === $field) {
echo ' selected';
}
echo '>' . $label . '</option>';
} ?>
</select>
<input type="checkbox" name="desc_order" id="desc_order" <?php if ($desc_order) echo 'checked'; ?>>
<label for="desc_order">Descending Order</label>
<input type="submit" value="Order">
</form>
<table>
<tr>
<?php foreach ($field_labels as $label) { ?>
<th><?php echo $label; ?></th>
<?php } ?>
</tr>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<?php foreach ($field_labels as $field => $label) { ?>
<td><?php echo $row[$field]; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php
}
function display_menu() {
html_prologue('Home'); ?>
<h2>Menu</h2>
<ul class="menu">
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>/insert">Insert Employee</a></li>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>/display">Display Employees</a></li>
</ul><?php
}
try {
if (!empty($_SERVER['PATH_INFO']) || $_SERVER['PATH_INFO'] !== '/') {
$path = $_SERVER['PATH_INFO'];
if ($path === '/insert') {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$dbh = connect_to_database();
insert_employee_record($dbh);
$dbh->close();
} else {
show_employee_form();
}
} elseif ($path === '/display') {
$dbh = connect_to_database();
display_employee_records($dbh);
$dbh->close();
} else {
display_menu();
echo '<p class="error">Path <code>' . htmlspecialchars($path) . '</code> was not found.</p>';
die();
}
} else {
display_menu();
}
} catch (mysqli_sql_exception $e) {
display_error($e->getMessage());
}
|