-
Notifications
You must be signed in to change notification settings - Fork 0
/
adminBus.php
200 lines (175 loc) · 6.74 KB
/
adminBus.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
<?php
session_start();
include ("database/connection.php");
include ("_admin.php");
$conn = new mysqli('localhost', 'root', '', 'travel');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle form submissions for insert, update, delete
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$bus_id = $_POST['bus_id'] ?? null;
$bus_name = $_POST['bus_name'] ?? null;
$source = $_POST['source'] ?? null;
$destination = $_POST['destination'] ?? null;
$fare = $_POST['fare'] ?? null;
$seats_available = $_POST['seats_available'] ?? null;
if (isset($_POST['insert'])) {
$sql = "INSERT INTO bus_details (bus_name, source, destination, fare, seats_available, Bus_id) VALUES ('$bus_name', '$source', '$destination', '$fare', '$seats_available', '$bus_id')";
if (!$conn->query($sql)) {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} elseif (isset($_POST['update']) && $bus_id) {
$sql = "UPDATE bus_details SET bus_name='$bus_name', source='$source', destination='$destination', fare='$fare', seats_available='$seats_available' WHERE Bus_id='$bus_id'";
if (!$conn->query($sql)) {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} elseif (isset($_POST['delete']) && $bus_id) {
$sql = "DELETE FROM bus_details WHERE Bus_id='$bus_id'";
if (!$conn->query($sql)) {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
// Fetch data from the bus_details table
$sql = "SELECT `bus_name`, `source`, `destination`, `fare`, `seats_available`, `Bus_id` FROM `bus_details`";
$result = $conn->query($sql);
if ($result === false) {
echo "Error: " . $sql . "<br>" . $conn->error;
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Bus Admin</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<style>
body {
font-family: Arial, sans-serif;
}
h1,
h2 {
color: #333;
}
.container {
display: flex;
justify-content: space-between;
align-items: flex-start;
}
.form-container {
flex: 1;
max-width: 300px;
margin-right: 20px;
}
.table-container {
flex: 2;
}
form {
margin-bottom: 20px;
}
table {
width: 100%;
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid #ccc;
}
th,
td {
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
input[type="text"],
input[type="number"] {
width: calc(100% - 20px);
padding: 8px;
margin: 5px 0;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>
<center>Bus Admin Page</center>
</h1>
<div class="container">
<div class="form-container">
<h2><?= isset($_POST['edit']) ? 'Edit Bus' : 'Insert New Bus' ?></h2>
<form method="post" action="">
<input type="hidden" name="<?= isset($_POST['edit']) ? 'update' : 'insert' ?>" value="1">
<?php if (isset($_POST['edit'])): ?>
<input type="hidden" name="bus_id" value="<?= $_POST['bus_id'] ?>">
<?php endif; ?>
Bus ID: <input type="text" name="bus_id" value="<?= $_POST['bus_id'] ?? '' ?>" required><br>
Bus Name: <input type="text" name="bus_name" value="<?= $_POST['bus_name'] ?? '' ?>" required><br>
Source: <input type="text" name="source" value="<?= $_POST['source'] ?? '' ?>" required><br>
Destination: <input type="text" name="destination" value="<?= $_POST['destination'] ?? '' ?>" required><br>
Fare: <input type="number" name="fare" value="<?= $_POST['fare'] ?? '' ?>" required><br>
Seats Available: <input type="number" name="seats_available" value="<?= $_POST['seats_available'] ?? '' ?>" required><br>
<input type="submit" value="<?= isset($_POST['edit']) ? 'Update' : 'Insert' ?>">
</form>
</div>
<div class="table-container">
<h2>
<center>Bus Details</center>
</h2>
<table>
<tr>
<th>Bus ID</th>
<th>Bus Name</th>
<th>Source</th>
<th>Destination</th>
<th>Fare</th>
<th>Seats Available</th>
<th>Actions</th>
</tr>
<?php if ($result->num_rows > 0): ?>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row["Bus_id"] ?></td>
<td><?= $row["bus_name"] ?></td>
<td><?= $row["source"] ?></td>
<td><?= $row["destination"] ?></td>
<td><?= $row["fare"] ?></td>
<td><?= $row["seats_available"] ?></td>
<td>
<form method="post" action="">
<input type="hidden" name="bus_id" value="<?= $row["Bus_id"] ?>">
<input type="hidden" name="bus_name" value="<?= $row["bus_name"] ?>">
<input type="hidden" name="source" value="<?= $row["source"] ?>">
<input type="hidden" name="destination" value="<?= $row["destination"] ?>">
<input type="hidden" name="fare" value="<?= $row["fare"] ?>">
<input type="hidden" name="seats_available" value="<?= $row["seats_available"] ?>">
<input type="submit" name="edit" value="Edit">
<input type="submit" name="delete" value="Delete" class="bg-danger">
</form>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="7">No results found</td>
</tr>
<?php endif; ?>
</table>
</div>
</div>
<?php $conn->close(); ?>
</body>
</html>