school management system project with source code in php

if ($_SERVER['REQUEST_METHOD'] == 'POST') { foreach ($_POST['attendance'] as $student_id => $status) { $check = mysqli_query($conn, "SELECT id FROM attendance WHERE student_id=$student_id AND date='$date'"); if (mysqli_num_rows($check) > 0) { mysqli_query($conn, "UPDATE attendance SET status='$status' WHERE student_id=$student_id AND date='$date'"); } else { mysqli_query($conn, "INSERT INTO attendance (student_id, class_id, date, status) VALUES ($student_id, $class_id, '$date', '$status')"); } } $success = "Attendance saved!"; }

if (mysqli_num_rows($result) == 1) { $user = mysqli_fetch_assoc($result); if (password_verify($password, $user['password'])) { $_SESSION['user_id'] = $user['id']; $_SESSION['role'] = $user['role']; $_SESSION['related_id'] = $user['related_id']; switch ($user['role']) { case 'admin': header("Location: admin/dashboard.php"); break; case 'teacher': header("Location: teacher/dashboard.php"); break; case 'student': header("Location: student/dashboard.php"); break; } } else { $error = "Invalid password"; } } else { $error = "User not found"; } } ?> <!-- HTML Form --> <form method="POST"> <input type="text" name="username" required> <input type="password" name="password" required> <button type="submit">Login</button> <?php if(isset($error)) echo "<p>$error</p>"; ?> </form> <?php require_once '../config/db_connection.php'; require_once '../includes/auth.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $roll_no = $_POST['roll_no']; $class_id = $_POST['class_id']; $section = $_POST['section']; $parent_mobile = $_POST['parent_mobile']; $address = $_POST['address']; $dob = $_POST['dob']; $admission_date = $_POST['admission_date'];

if (mysqli_query($conn, $query)) { $student_id = mysqli_insert_id($conn); $username = strtolower($first_name . "." . $last_name); $default_password = password_hash($roll_no, PASSWORD_DEFAULT); mysqli_query($conn, "INSERT INTO users (username, password, role, related_id) VALUES ('$username', '$default_password', 'student', $student_id)"); $success = "Student added successfully. Username: $username, Password: $roll_no"; } else { $error = "Error: " . mysqli_error($conn); } }

$query = "INSERT INTO students (first_name, last_name, roll_no, class_id, section, parent_mobile, address, dob, admission_date) VALUES ('$first_name', '$last_name', '$roll_no', $class_id, '$section', '$parent_mobile', '$address', '$dob', '$admission_date')";

if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> <?php session_start(); if (!isset($_SESSION['user_id'])) { header("Location: ../login.php"); exit(); } ?> 5.3 Login Script ( login.php ) <?php session_start(); require_once 'config/db_connection.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = mysqli_real_escape_string($conn, $_POST['username']); $password = $_POST['password'];

// Fetch recent attendance $attendance = mysqli_query($conn, "SELECT date, status FROM attendance WHERE student_id=$student_id ORDER BY date DESC LIMIT 10");

<table> <?php while($s = mysqli_fetch_assoc($students)): ?> <tr> <td><?= $s['first_name'] . " " . $s['last_name'] ?></td> <td> <select name="attendance[<?= $s['id'] ?>]"> <option value="present">Present</option> <option value="absent">Absent</option> <option value="late">Late</option> </select> </td> </tr> <?php endwhile; ?> </table> <button type="submit">Save Attendance</button> </form> <?php require_once '../config/db_connection.php'; require_once '../includes/auth.php'; $student_id = $_SESSION['related_id'];

$query = "SELECT * FROM users WHERE username='$username'"; $result = mysqli_query($conn, $query);

// Get classes taught by this teacher $classes_taught = mysqli_query($conn, "SELECT DISTINCT class_id FROM subjects WHERE teacher_id=$teacher_id"); $students = []; if ($class_id) { $students = mysqli_query($conn, "SELECT * FROM students WHERE class_id=$class_id"); } ?> <form method="POST"> <select name="class_id" onchange="this.form.submit()"> <option value="">Select Class</option> <?php while($row = mysqli_fetch_assoc($classes_taught)): ?> <option value="<?= $row['class_id'] ?>" <?= $class_id == $row['class_id'] ? 'selected' : '' ?>> Class <?= $row['class_id'] ?> </option> <?php endwhile; ?> </select>

// Fetch marks $marks = mysqli_query($conn, "SELECT m.exam_type, s.subject_name, m.marks_obtained, m.max_marks FROM marks m JOIN subjects s ON m.subject_id = s.id WHERE m.student_id=$student_id"); ?> <h1>Welcome, <?= $student['first_name'] ?></h1> <h3>Recent Attendance</h3> <ul> <?php while($row = mysqli_fetch_assoc($attendance)): ?> <li><?= $row['date'] ?> - <?= $row['status'] ?></li> <?php endwhile; ?> </ul>

// Fetch classes for dropdown $classes = mysqli_query($conn, "SELECT * FROM classes"); ?> <!-- HTML form for adding student --> <?php require_once '../config/db_connection.php'; require_once '../includes/auth.php'; $teacher_id = $_SESSION['related_id']; $class_id = $_GET['class_id'] ?? null; $date = date('Y-m-d');

1. Introduction A School Management System automates daily administrative tasks like student registration, attendance tracking, grade management, fee collection, and teacher assignment.

// Fetch student details $student = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM students WHERE id=$student_id"));

CREATE DATABASE school_management; USE school_management; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, role ENUM('admin', 'teacher', 'student', 'parent') NOT NULL, related_id INT NOT NULL, -- student_id or teacher_id created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); Table: students CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, roll_no VARCHAR(20) UNIQUE, class_id INT, section VARCHAR(10), parent_mobile VARCHAR(15), address TEXT, dob DATE, admission_date DATE ); Table: teachers CREATE TABLE teachers ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE, mobile VARCHAR(15), qualification VARCHAR(100), address TEXT ); Table: classes CREATE TABLE classes ( id INT AUTO_INCREMENT PRIMARY KEY, class_name VARCHAR(20) NOT NULL, -- e.g., 1, 2, 3, etc. numeric_name INT ); Table: subjects CREATE TABLE subjects ( id INT AUTO_INCREMENT PRIMARY KEY, subject_name VARCHAR(50) NOT NULL, class_id INT, teacher_id INT, FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE CASCADE, FOREIGN KEY (teacher_id) REFERENCES teachers(id) ON DELETE SET NULL ); Table: attendance CREATE TABLE attendance ( id INT AUTO_INCREMENT PRIMARY KEY, student_id INT, class_id INT, date DATE, status ENUM('present', 'absent', 'late') DEFAULT 'absent', FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE, FOREIGN KEY (class_id) REFERENCES classes(id) ); Table: marks CREATE TABLE marks ( id INT AUTO_INCREMENT PRIMARY KEY, student_id INT, subject_id INT, exam_type ENUM('FA1', 'FA2', 'SA1', 'SA2') NOT NULL, marks_obtained INT, max_marks INT DEFAULT 100, FOREIGN KEY (student_id) REFERENCES students(id), FOREIGN KEY (subject_id) REFERENCES subjects(id) ); Table: fees CREATE TABLE fees ( id INT AUTO_INCREMENT PRIMARY KEY, student_id INT, amount_due DECIMAL(10,2), amount_paid DECIMAL(10,2), due_date DATE, payment_date DATE, status ENUM('paid', 'pending', 'partial'), FOREIGN KEY (student_id) REFERENCES students(id) ); 4. Project Structure school-management/ │ ├── config/ │ └── db_connection.php ├── includes/ │ ├── header.php │ ├── footer.php │ └── auth.php ├── assets/ │ ├── css/ │ ├── js/ │ └── images/ ├── admin/ │ ├── dashboard.php │ ├── manage_students.php │ ├── manage_teachers.php │ ├── manage_classes.php │ ├── manage_subjects.php │ └── fee_report.php ├── teacher/ │ ├── dashboard.php │ ├── attendance.php │ ├── marks_entry.php │ └── my_classes.php ├── student/ │ └── dashboard.php ├── login.php ├── logout.php └── index.php 5. Core Source Code 5.1 Database Connection ( config/db_connection.php ) <?php $host = 'localhost'; $user = 'root'; $password = ''; $database = 'school_management'; $conn = mysqli_connect($host, $user, $password, $database);

Don't miss the top ESG stories!

Don't miss the top ESG stories!

Join the ESG Today daily newsletter and get all the top ESG stories, like this one.

Subscribe now below!

You have Successfully Subscribed!