setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Create database if it doesn't exist $pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbname`"); // Now connect to the specific database $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("Database connection failed: " . $e->getMessage()); } // Create tables if they don't exist $createTables = " CREATE TABLE IF NOT EXISTS students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, phone VARCHAR(20), course_level ENUM('PUC', 'UG', 'PG') NOT NULL, course_name VARCHAR(100) NOT NULL, year INT NOT NULL, photo_path VARCHAR(255), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS courses ( id INT AUTO_INCREMENT PRIMARY KEY, course_level ENUM('PUC', 'UG', 'PG') NOT NULL, course_name VARCHAR(100) NOT NULL, years INT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS admins ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); "; try { $pdo->exec($createTables); } catch (PDOException $e) { // Tables might already exist } // Insert default admin if no admin exists $checkAdmin = $pdo->query("SELECT COUNT(*) FROM admins"); if ($checkAdmin->fetchColumn() == 0) { $default_password = password_hash('admin123', PASSWORD_DEFAULT); $stmt = $pdo->prepare("INSERT INTO admins (username, password) VALUES (?, ?)"); $stmt->execute(['admin', $default_password]); } // Insert sample courses if empty $checkCourses = $pdo->query("SELECT COUNT(*) FROM courses"); if ($checkCourses->fetchColumn() == 0) { $sampleCourses = [ // PUC Courses - 2 Years ['PUC', 'ABES', 2], ['PUC', 'ABECs', 2], ['PUC', 'ABMS', 2], ['PUC', 'CEBA', 2], // NEW: Added CEBA course ['PUC', 'HEBA', 2], ['PUC', 'HEPS', 2], // NEW: Added HEPS course ['PUC', 'PCMB', 2], ['PUC', 'PCMCs', 2], ['PUC', 'PCME', 2], // UG Courses - 3 Years ['UG', 'B.Com', 3], ['UG', 'BBA', 3], ['UG', 'BCA', 3], ['UG', 'B.Sc-PMCs', 3], ['UG', 'B.Sc-BGBt', 3], // PG Courses - 2 Years ['PG', 'M.Com', 2], ['PG', 'M.Sc Computer Science', 2] ]; $stmt = $pdo->prepare("INSERT INTO courses (course_level, course_name, years) VALUES (?, ?, ?)"); foreach ($sampleCourses as $course) { $stmt->execute($course); } } // Initialize message variable $message = ''; // Handle Admin Login $login_error = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) { $username = $_POST['username']; $password = $_POST['password']; $stmt = $pdo->prepare("SELECT * FROM admins WHERE username = ?"); $stmt->execute([$username]); $admin = $stmt->fetch(); if ($admin && password_verify($password, $admin['password'])) { $_SESSION['admin_logged_in'] = true; $_SESSION['admin_username'] = $admin['username']; header('Location: index.php'); exit(); } else { $login_error = "Invalid username or password!"; } } // Handle Logout if (isset($_GET['logout'])) { session_destroy(); header('Location: index.php'); exit(); } // Check if admin is logged in $is_admin = isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true; // Handle Course Management if ($_SERVER['REQUEST_METHOD'] === 'POST' && $is_admin) { // Add Course if (isset($_POST['add_course'])) { $course_level = $_POST['course_level']; $course_name = trim($_POST['course_name']); $years = intval($_POST['years']); try { $stmt = $pdo->prepare("INSERT INTO courses (course_level, course_name, years) VALUES (?, ?, ?)"); $stmt->execute([$course_level, $course_name, $years]); $message = "Course added successfully!"; } catch (PDOException $e) { $message = "Error adding course: " . $e->getMessage(); } } // Edit Course if (isset($_POST['edit_course'])) { $id = intval($_POST['course_id']); $course_level = $_POST['course_level']; $course_name = trim($_POST['course_name']); $years = intval($_POST['years']); try { $stmt = $pdo->prepare("UPDATE courses SET course_level = ?, course_name = ?, years = ? WHERE id = ?"); $stmt->execute([$course_level, $course_name, $years, $id]); $message = "Course updated successfully!"; } catch (PDOException $e) { $message = "Error updating course: " . $e->getMessage(); } } // Delete Course if (isset($_POST['delete_course'])) { $id = intval($_POST['course_id']); try { // Get course details first $stmt = $pdo->prepare("SELECT course_level, course_name FROM courses WHERE id = ?"); $stmt->execute([$id]); $course = $stmt->fetch(); if ($course) { // Check if course has students $stmt = $pdo->prepare("SELECT COUNT(*) FROM students WHERE course_level = ? AND course_name = ?"); $stmt->execute([$course['course_level'], $course['course_name']]); $count = $stmt->fetchColumn(); if ($count > 0) { $message = "Cannot delete course '$course[course_name]'. It has $count students enrolled. Remove students first."; } else { $stmt = $pdo->prepare("DELETE FROM courses WHERE id = ?"); $stmt->execute([$id]); $message = "Course deleted successfully!"; } } } catch (PDOException $e) { $message = "Error deleting course: " . $e->getMessage(); } } } // Handle Student Management if ($_SERVER['REQUEST_METHOD'] === 'POST' && $is_admin) { if (isset($_POST['action'])) { // Add Student if ($_POST['action'] === 'add') { $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $course_level = $_POST['course_level']; $course_name = $_POST['course_name']; $year = $_POST['year']; // Handle photo upload $photo_path = ''; if (isset($_FILES['photo']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) { $upload_dir = 'uploads/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0777, true); } $file_extension = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION); $photo_path = $upload_dir . uniqid() . '.' . $file_extension; move_uploaded_file($_FILES['photo']['tmp_name'], $photo_path); } try { $stmt = $pdo->prepare("INSERT INTO students (name, email, phone, course_level, course_name, year, photo_path) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $email, $phone, $course_level, $course_name, $year, $photo_path]); $message = "Student added successfully!"; } catch (PDOException $e) { $message = "Error: " . $e->getMessage(); } } // Delete Student if ($_POST['action'] === 'delete') { $id = $_POST['id']; try { // Get photo path to delete file $stmt = $pdo->prepare("SELECT photo_path FROM students WHERE id = ?"); $stmt->execute([$id]); $student = $stmt->fetch(); if ($student && $student['photo_path'] && file_exists($student['photo_path'])) { unlink($student['photo_path']); } $stmt = $pdo->prepare("DELETE FROM students WHERE id = ?"); $stmt->execute([$id]); $message = "Student deleted successfully!"; } catch (PDOException $e) { $message = "Error: " . $e->getMessage(); } } // Edit Student if ($_POST['action'] === 'edit') { $id = $_POST['id']; $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $course_level = $_POST['course_level']; $course_name = $_POST['course_name']; $year = $_POST['year']; try { // Handle photo upload for edit $photo_path = null; if (isset($_FILES['photo']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) { $upload_dir = 'uploads/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0777, true); } $file_extension = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION); $photo_path = $upload_dir . uniqid() . '.' . $file_extension; move_uploaded_file($_FILES['photo']['tmp_name'], $photo_path); // Delete old photo $stmt = $pdo->prepare("SELECT photo_path FROM students WHERE id = ?"); $stmt->execute([$id]); $old_student = $stmt->fetch(); if ($old_student && $old_student['photo_path'] && file_exists($old_student['photo_path'])) { unlink($old_student['photo_path']); } } if ($photo_path) { $stmt = $pdo->prepare("UPDATE students SET name=?, email=?, phone=?, course_level=?, course_name=?, year=?, photo_path=? WHERE id=?"); $stmt->execute([$name, $email, $phone, $course_level, $course_name, $year, $photo_path, $id]); } else { $stmt = $pdo->prepare("UPDATE students SET name=?, email=?, phone=?, course_level=?, course_name=?, year=? WHERE id=?"); $stmt->execute([$name, $email, $phone, $course_level, $course_name, $year, $id]); } $message = "Student updated successfully!"; } catch (PDOException $e) { $message = "Error: " . $e->getMessage(); } } } } // Get course view parameters $view_level = isset($_GET['view_level']) ? $_GET['view_level'] : ''; $view_course = isset($_GET['view_course']) ? $_GET['view_course'] : ''; // Get students for specific course view $course_students = []; $course_info = null; if ($view_level && $view_course) { $stmt = $pdo->prepare("SELECT * FROM students WHERE course_level = ? AND course_name = ? ORDER BY year, name"); $stmt->execute([$view_level, $view_course]); $course_students = $stmt->fetchAll(); // Get course info $stmt = $pdo->prepare("SELECT * FROM courses WHERE course_level = ? AND course_name = ?"); $stmt->execute([$view_level, $view_course]); $course_info = $stmt->fetch(); } // Get all courses with student counts $courseStats = $pdo->query(" SELECT c.id, c.course_level, c.course_name, c.years, COUNT(s.id) as student_count FROM courses c LEFT JOIN students s ON c.course_name = s.course_name AND c.course_level = s.course_level GROUP BY c.id, c.course_level, c.course_name, c.years ORDER BY c.course_level, c.course_name ")->fetchAll(); // Get all courses for management $allCourses = $pdo->query("SELECT * FROM courses ORDER BY course_level, course_name")->fetchAll(); // Get courses for dropdowns $courses = $pdo->query("SELECT DISTINCT course_level, course_name, years FROM courses ORDER BY course_level, course_name")->fetchAll(); // Get student for editing $edit_student = null; if (isset($_GET['edit'])) { $stmt = $pdo->prepare("SELECT * FROM students WHERE id = ?"); $stmt->execute([$_GET['edit']]); $edit_student = $stmt->fetch(); } // Get course for editing $edit_course = null; if (isset($_GET['edit_course'])) { $stmt = $pdo->prepare("SELECT * FROM courses WHERE id = ?"); $stmt->execute([$_GET['edit_course']]); $edit_course = $stmt->fetch(); } // Get total counts $totalStudents = $pdo->query("SELECT COUNT(*) FROM students")->fetchColumn(); $totalCourses = $pdo->query("SELECT COUNT(*) FROM courses")->fetchColumn(); $pucCount = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'PUC'")->fetchColumn(); $ugCount = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'UG'")->fetchColumn(); $pgCount = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'PG'")->fetchColumn(); // Get year-wise counts $pucYear1 = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'PUC' AND year = 1")->fetchColumn(); $pucYear2 = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'PUC' AND year = 2")->fetchColumn(); $ugYear1 = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'UG' AND year = 1")->fetchColumn(); $ugYear2 = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'UG' AND year = 2")->fetchColumn(); $ugYear3 = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'UG' AND year = 3")->fetchColumn(); $pgYear1 = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'PG' AND year = 1")->fetchColumn(); $pgYear2 = $pdo->query("SELECT COUNT(*) FROM students WHERE course_level = 'PG' AND year = 2")->fetchColumn(); ?> Student Management System - Admin Dashboard

🎓 Student Management System

Admin Dashboard - Manage Students & Courses
👑 Admin:
🚪 Logout
Total Students
Total Courses
PUC Students
I: | II:
UG Students
I: | II: | III:
PG Students
I: | II:

⚙️ Admin Dashboard

➕ Add / Edit Students

Current: View Photo
Cancel

Cancel

📋 All Courses

0): ?>
prepare("SELECT COUNT(*) FROM students WHERE course_level = ? AND course_name = ?"); $stmt->execute([$course['course_level'], $course['course_name']]); $studentCount = $stmt->fetchColumn(); ?>
ID Level Course Name Duration Students Actions
Year 1 ? 's' : ''; ?> student
Edit

No courses found. Add your first course!

📊 Course-wise Student Distribution

'Pre-University Course (2 Years)', 'UG' => 'Under Graduate (3 Years)', 'PG' => 'Post Graduate (2 Years)' ]; foreach ($courseStats as $course): if ($current_level !== $course['course_level']): if ($current_level !== ''): echo '
'; endif; $current_level = $course['course_level']; ?>

-

Year Program (I & II Year) (I, II & III Year)
View Students →
'; endif; ?>

Year Program (I & II Year) (I, II & III Year) | Total Students:
← Back to Dashboard
0): ?>
Photo Name Email Phone Year Added Actions
Photo
👤
Edit

No students enrolled in this course yet.

Use the "Add New Student" form in the Admin Dashboard to add students to this course.