8000 Update view_attendance.php · Abhijeetbyte/SQLite-PHP-Database@39e74bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 39e74bd

Browse files
authored
Update view_attendance.php
1 parent d3d7db8 commit 39e74bd

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

view_attendance.php

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,46 @@
11
<?php
2-
// Function to sanitize input data
3-
function sanitize_input($data) {
4-
return htmlspecialchars(trim($data));
5-
}
6-
72
try {
8-
// Connect to SQLite database
3+
// Check if required GET fields are set and not empty
4+
if (empty($ 8000 _GET['roll_number'])) {
5+
throw new Exception('Roll number is required.');
6+
}
7+
8+
// Connect to the SQLite database
99
$db = new PDO('sqlite:students.db');
10-
11-
// Set error mode to throw exceptions
10+
11+
// Set error mode to exceptions
1212
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1313

14-
// Sanitize and validate input
15-
$roll_no = sanitize_input($_GET['roll_no']);
14+
// Sanitize user input
15+
$roll_number = filter_input(INPUT_GET, 'roll_number', FILTER_SANITIZE_STRING);
1616

17-
// Check if roll number is provided
18-
if (empty($roll_no)) {
19-
throw new Exception("Roll number is required.");
20-
}
17+
// Prepare an SQL statement for safe selection
18+
$stmt = $db->prepare("SELECT * FROM students WHERE roll_number = :roll_number");
19+
20+
// Bind the roll_number parameter
21+
$stmt->bindParam(':roll_number', $roll_number);
2122

22-
// Prepare SQL statement to fetch attendance data
23-
$stmt = $db->prepare("SELECT * FROM Attendance WHERE roll_no = :roll_no");
24-
$stmt->bindParam(':roll_no', $roll_no);
25-
26-
// Execute SQL statement
23+
// Execute the statement
2724
$stmt->execute();
2825

29-
// Fetch all rows as associative array
30-
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
31-
32-
// Display attendance data if found, otherwise show message
33-
if ($results) {
34-
echo "<table border='1' style='margin: 20px auto; border-collapse: collapse;'>";
35-
echo "<tr><th>ID</th><th>Roll No</th><th>Student Name</th><th>Date</th></tr>";
36-
foreach ($results as $row) {
37-
echo "<tr>";
38-
echo "<td>" . htmlspecialchars($row['id']) . "</td>";
39-
echo "<td>" . htmlspecialchars($row['roll_no']) . "</td>";
40-
echo "<td>" . htmlspecialchars($row['student_name']) . "</td>";
41-
echo "<td>" . htmlspecialchars($row['date']) . "</td>";
42-
echo "</tr>";
43-
}
44-
echo "</table>";
26+
// Fetch the result
27+
$student = $stmt->fetch(PDO::FETCH_ASSOC);
28+
29+
// Display the result if found
30+
if ($student) {
31+
echo "Roll Number: " . htmlspecialchars($student['roll_number']) . "<br>";
32+
echo "Name: " . htmlspecialchars($student['name']) . "<br>";
33+
echo "Date: " . htmlspecialchars($student['date']) . "<br>";
4534
} else {
46-
echo "No records found for roll number " . htmlspecialchars($roll_no);
35+
echo "No attendance record found for the provided roll number.";
4736
}
4837
} catch (Exception $e) {
49-
// Log error to server logs
50-
error_log("Error: " . $e->getMessage());
51-
52-
// Error message for users
53-
echo "An error occurred. Please try again later.";
38+
// Log the error message
39+
error_log($e->getMessage());
40+
echo "An error occurred: " . htmlspecialchars($e->getMessage());
41+
} catch (PDOException $e) {
42+
// Log the error message
43+
error_log($e->getMessage());
44+
echo "An error occurred while retrieving attendance data.";
5445
}
5546
?>

0 commit comments

Comments
 (0)
0