-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudents.php
More file actions
46 lines (42 loc) · 1.72 KB
/
Copy pathstudents.php
File metadata and controls
46 lines (42 loc) · 1.72 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
<?php
require 'config/db.php';
require 'includes/functions.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$studentName = trim($_POST['student_name'] ?? '');
$rollNo = trim($_POST['roll_no'] ?? '');
$className = trim($_POST['class_name'] ?? '');
if ($studentName === '' || $rollNo === '' || $className === '') {
redirect_with_message('students.php', 'All student fields are required.', 'error');
}
$stmt = $pdo->prepare("INSERT INTO students (student_name, roll_no, class_name) VALUES (?, ?, ?)");
try {
$stmt->execute([$studentName, $rollNo, $className]);
redirect_with_message('students.php', 'Student added successfully.');
} catch (PDOException $e) {
redirect_with_message('students.php', 'Roll number must be unique.', 'error');
}
}
$students = get_students($pdo);
include 'includes/header.php';
?>
<h2>Manage Students</h2>
<form method="POST">
<div class="grid">
<div><input type="text" name="student_name" placeholder="Student Name" required></div>
<div><input type="text" name="roll_no" placeholder="Roll No" required></div>
<div><input type="text" name="class_name" placeholder="Class" required></div>
<div><button type="submit">Add Student</button></div>
</div>
</form>
<table>
<tr><th>ID</th><th>Name</th><th>Roll No</th><th>Class</th></tr>
<?php foreach ($students as $student): ?>
<tr>
<td><?php echo h($student['id']); ?></td>
<td><?php echo h($student['student_name']); ?></td>
<td><?php echo h($student['roll_no']); ?></td>
<td><?php echo h($student['class_name']); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php include 'includes/footer.php'; ?>