-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
151 lines (130 loc) · 5.05 KB
/
index.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
<?php
include "dbinfo.inc.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Employee Details</title>
<!-- Bootstrap CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center mb-4">Employee Details</h1>
<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Connect to MySQL and select the database
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) {
echo "<div class='alert alert-danger'>Failed to connect to MySQL: " . mysqli_connect_error() . "</div>";
exit();
}
$database = mysqli_select_db($connection, DB_DATABASE);
if (!$database) {
echo "<div class='alert alert-danger'>Failed to select database: " . mysqli_error($connection) . "</div>";
exit();
}
// Ensure that the EMPLOYEES table exists
verifyEmployeesTable($connection, DB_DATABASE);
// If input fields are populated, add a row to the EMPLOYEES table
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$employee_name = htmlentities($_POST['NAME']);
$employee_address = htmlentities($_POST['ADDRESS']);
if (strlen($employee_name) || strlen($employee_address)) {
addEmployee($connection, $employee_name, $employee_address);
}
}
?>
<!-- Input form -->
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" class="mb-5">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="NAME" maxlength="45" required>
</div>
<div class="form-group col-md-6">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" name="ADDRESS" maxlength="90" required>
</div>
</div>
<button type="submit" class="btn btn-primary">Add Data</button>
</form>
<!-- Display table data -->
<table class="table table-striped table-bordered">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($connection, "SELECT * FROM EMPLOYEES");
if (!$result) {
echo "<div class='alert alert-danger'>Error fetching data: " . mysqli_error($connection) . "</div>";
} else {
while ($query_data = mysqli_fetch_row($result)) {
echo "<tr>";
echo "<td>", $query_data[0], "</td>",
"<td>", $query_data[1], "</td>",
"<td>", $query_data[2], "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
<?php
// Clean up
mysqli_free_result($result);
mysqli_close($connection);
?>
</div>
<!-- Bootstrap JS and dependencies -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
<?php
// Function to add an employee to the table
function addEmployee($connection, $name, $address) {
$n = mysqli_real_escape_string($connection, $name);
$a = mysqli_real_escape_string($connection, $address);
$query = "INSERT INTO EMPLOYEES (NAME, ADDRESS) VALUES ('$n', '$a');";
if (!mysqli_query($connection, $query)) {
echo "<div class='alert alert-danger'>Error adding employee data: " . mysqli_error($connection) . "</div>";
} else {
echo "<div class='alert alert-success'>Employee added successfully.</div>";
}
}
// Function to check whether the table exists and, if not, create it
function verifyEmployeesTable($connection, $dbName) {
if (!tableExists("EMPLOYEES", $connection, $dbName)) {
$query = "CREATE TABLE EMPLOYEES (
ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(45) NOT NULL,
ADDRESS VARCHAR(90) NOT NULL
)";
if (!mysqli_query($connection, $query)) {
echo "<div class='alert alert-danger'>Error creating table: " . mysqli_error($connection) . "</div>";
}
}
}
// Function to check for the existence of a table
function tableExists($tableName, $connection, $dbName) {
$t = mysqli_real_escape_string($connection, $tableName);
$d = mysqli_real_escape_string($connection, $dbName);
$checkTable = mysqli_query($connection,
"SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");
if (mysqli_num_rows($checkTable) > 0) {
return true;
}
return false;
}
?>