36228-vm/students.php
2025-11-24 21:57:09 +00:00

140 lines
6.0 KiB
PHP

<?php
require_once 'db/config.php';
// Initialize database schema
db_init();
$pdo = db_connect();
$message = '';
$error = '';
// Handle form submission for adding a new student
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
$date_of_birth = trim($_POST['date_of_birth'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone_number = trim($_POST['phone_number'] ?? '');
if (!empty($first_name) && !empty($last_name)) {
try {
$sql = "INSERT INTO students (first_name, last_name, date_of_birth, email, phone_number) VALUES (?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$first_name, $last_name, $date_of_birth, $email, $phone_number]);
$message = "Student added successfully!";
} catch (PDOException $e) {
$error = "Error adding student: " . $e->getMessage();
}
} else {
$error = "Please provide at least a first and last name.";
}
}
// Fetch all students
try {
$stmt = $pdo->query("SELECT id, first_name, last_name, date_of_birth FROM students ORDER BY created_at DESC");
$students = $stmt->fetchAll();
} catch (PDOException $e) {
$error = "Error fetching students: " . $e->getMessage();
$students = [];
}
include 'layout_header.php';
?>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0 text-gray-800">Student Management</h1>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addStudentModal">
<i data-feather="plus" class="me-2"></i>Add Student
</button>
</div>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead class="table-light">
<tr>
<th>ID</th>
<th>Name</th>
<th>Date of Birth</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($students)): ?>
<tr>
<td colspan="4" class="text-center">No students found.</td>
</tr>
<?php else: ?>
<?php foreach ($students as $student): ?>
<tr>
<td><?php echo htmlspecialchars($student['id']); ?></td>
<td><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></td>
<td><?php echo htmlspecialchars($student['date_of_birth']); ?></td>
<td>
<a href="#" class="btn btn-sm btn-outline-primary"><i data-feather="edit-2" class="me-1"></i> Edit</a>
<a href="#" class="btn btn-sm btn-outline-danger"><i data-feather="trash-2" class="me-1"></i> Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Add Student Modal -->
<div class="modal fade" id="addStudentModal" tabindex="-1" aria-labelledby="addStudentModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addStudentModalLabel">Add New Student</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="students.php" method="POST">
<div class="row">
<div class="col-md-6 mb-3">
<label for="first_name" class="form-label">First Name</label>
<input type="text" class="form-control" id="first_name" name="first_name" required>
</div>
<div class="col-md-6 mb-3">
<label for="last_name" class="form-label">Last Name</label>
<input type="text" class="form-control" id="last_name" name="last_name" required>
</div>
</div>
<div class="mb-3">
<label for="date_of_birth" class="form-label">Date of Birth</label>
<input type="date" class="form-control" id="date_of_birth" name="date_of_birth">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email">
</div>
<div class="mb-3">
<label for="phone_number" class="form-label">Phone Number</label>
<input type="tel" class="form-control" id="phone_number" name="phone_number">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save Student</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include 'layout_footer.php'; ?>