-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_train_data.php
More file actions
75 lines (64 loc) · 3.53 KB
/
fetch_train_data.php
File metadata and controls
75 lines (64 loc) · 3.53 KB
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
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbms";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$fromValue = $_GET['from'];
$toValue = $_GET['to'];
$dateValue = $_GET['date'];
// Using prepared statement to prevent SQL injection
$stmt = $conn->prepare("SELECT * FROM train_details WHERE startPoint = ? AND destinationPoint = ? ORDER BY RAND() LIMIT 50");
$stmt->bind_param("ss", $fromValue, $toValue);
if ($stmt->execute()) {
$result = $stmt->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_object()) {
echo "<tr class='odd gradeX even gradeC odd gradeA'>";
echo "<td>" . $row->trainNumber . "</td>";
echo "<td>" . $row->trainName . "</td>";
echo "<td>" . $row->stationName . "</td>";
echo "<td>" . $row->startPoint . "</td>";
echo "<td>" . $row->destinationPoint . "</td>";
echo "<td>" . $row->arrivalTime . "</td>";
echo "<td>" . $row->departureTime . "</td>";
echo "<td>" . $row->distance . "</td>";
echo "<td>" . $row->Seats . "</td>";
echo "<td>" . $row->Fare . "</td>";
echo "<td><a href='book_ticket.php?train_number=" . $row->trainNumber . "&train_name=" . urlencode($row->trainName) . "&train_station=" . urlencode($row->stationName) . "&train_startPoint=" . urlencode($row->startPoint) . "&destinationPoint=" . urlencode($row->destinationPoint) . "&arrivalTime=" . urlencode($row->arrivalTime) . "&train_fare=" . urlencode($row->Fare) . "&departureTime=" . urlencode($row->departureTime) . "&distance=" . urlencode($row->distance) . "&Seats=" . urlencode($row->Seats) . "&JourneyDate=" . urlencode($dateValue) . "' class=\"button three rounded\">Book</a></td>";
echo "</tr>";
}
} else {
echo nl2br("<span style='color:red;'>No Data Found!\nAvailable Trains.</span>");
// Close the previous statement
$stmt->close();
// Perform a new query to get all available trains
$allTrainsQuery = "SELECT * FROM train_details";
$allTrainsResult = $conn->query($allTrainsQuery);
if ($allTrainsResult) {
while ($row = $allTrainsResult->fetch_object()) {
echo "<tr class='odd gradeX even gradeC odd gradeA'>";
echo "<td>" . $row->trainNumber . "</td>";
echo "<td>" . $row->trainName . "</td>";
echo "<td>" . $row->stationName . "</td>";
echo "<td>" . $row->startPoint . "</td>";
echo "<td>" . $row->destinationPoint . "</td>";
echo "<td>" . $row->arrivalTime . "</td>";
echo "<td>" . $row->departureTime . "</td>";
echo "<td>" . $row->distance . "</td>";
echo "<td>" . $row->Seats . "</td>";
echo "<td>" . $row->Fare . "</td>";
echo "<td><a href='book_ticket.php?train_number=" . $row->trainNumber . "&train_name=" . urlencode($row->trainName) . "&train_station=" . urlencode($row->stationName) . "&train_startPoint=" . urlencode($row->startPoint) . "&destinationPoint=" . urlencode($row->destinationPoint) . "&arrivalTime=" . urlencode($row->arrivalTime) . "&train_fare=" . urlencode($row->Fare) . "&departureTime=" . urlencode($row->departureTime) . "&distance=" . urlencode($row->distance) . "&Seats=" . urlencode($row->Seats) . "&JourneyDate=" . urlencode($dateValue) . "' class=\"button three rounded\">Book</a></td>";
echo "</tr>";
}
} else {
echo "Error loading all available trains: " . $conn->error;
}
}
} else {
echo "Error executing the query: " . $stmt->error;
}
$conn->close();