148 lines
6.6 KiB
PHP
148 lines
6.6 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
// If the user is not logged in, redirect to the login page...
|
|
if (!isset($_SESSION['loggedin'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
// Role check
|
|
if ($_SESSION['role'] === 'viewer') {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
$page_title = "Add New Student";
|
|
$page_description = "Admission form to add a new student to the database.";
|
|
$notification = null;
|
|
|
|
try {
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
// Create table if it doesn't exist
|
|
$pdo = db();
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS students (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
first_name VARCHAR(100) NOT NULL,
|
|
last_name VARCHAR(100) NOT NULL,
|
|
date_of_birth DATE NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);");
|
|
|
|
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']);
|
|
|
|
if (empty($first_name) || empty($last_name) || empty($date_of_birth)) {
|
|
$notification = ["type" => "danger", "message" => "All fields are required."];
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO students (first_name, last_name, date_of_birth) VALUES (?, ?, ?)");
|
|
if ($stmt->execute([$first_name, $last_name, $date_of_birth])) {
|
|
$notification = ["type" => "success", "message" => "Student added successfully!"];
|
|
} else {
|
|
$notification = ["type" => "danger", "message" => "Error: Could not add student."];
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
$notification = ["type" => "danger", "message" => "Database error: " . $e->getMessage()];
|
|
error_log("DB Error: " . $e->getMessage());
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($page_title) ?> - <?= htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'WebApp') ?></title>
|
|
<meta name="description" content="<?= htmlspecialchars($page_description) ?>">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body class="bg-light">
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
|
|
<div class="container-fluid">
|
|
<a class="navbar-brand" href="index.php"><?= htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'WebApp') ?></a>
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav ms-auto">
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="#">Welcome, <?= htmlspecialchars($_SESSION['username']) ?>!</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="logout.php">Logout</a>
|
|
</li>
|
|
<?php if ($_SESSION['role'] === 'admin'): ?>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="admin.php">Admin</a>
|
|
</li>
|
|
<?php endif; ?>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container my-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8 col-lg-6">
|
|
<div class="card border-0 shadow-lg">
|
|
<div class="card-body p-4 p-md-5">
|
|
<h1 class="h3 card-title text-center mb-4">New Student Admission</h1>
|
|
<form action="add_student.php" method="POST" class="needs-validation" novalidate>
|
|
<div class="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 class="invalid-feedback">Please enter a first name.</div>
|
|
</div>
|
|
<div class="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 class="invalid-feedback">Please enter a last name.</div>
|
|
</div>
|
|
<div class="mb-4">
|
|
<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" required>
|
|
<div class="invalid-feedback">Please enter a valid date of birth.</div>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary btn-lg">Add Student</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<div class="toast-container position-fixed bottom-0 end-0 p-3">
|
|
<div id="notificationToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="toast-header">
|
|
<strong class="me-auto">Notification</strong>
|
|
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
<div class="toast-body">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer class="text-center py-4 text-muted">
|
|
<p>© <?= date('Y') ?> <?= htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'WebApp') ?>. All Rights Reserved.</p>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js"></script>
|
|
<?php if ($notification): ?>
|
|
<script>
|
|
showToast(<?= json_encode($notification['message']) ?>, '<?= htmlspecialchars($notification['type']) ?>');
|
|
</script>
|
|
<?php endif; ?>
|
|
</body>
|
|
</html>
|