采用PHP和MySQL的学校管理系统

425 阅读5分钟

学校管理系统(SMS)是一个网络应用程序,通常用于学校管理教师、学生、班级、科目、部门、学生出勤等。

因此,如果你是一个PHP开发人员,并想用PHP开发学校管理系统,那么你就来对地方了。在我们之前的教程中,你已经学会了如何用PHP和MYSQL开发在线投票系统。在本教程中,你将学习如何用PHP和MySQL开发一个学校管理系统

我们将以简单的步骤介绍本教程,开发学校管理系统的现场演示,涵盖一些主要功能,如管理教师、学生、班级、科目、部门、学生出勤等。这是一个非常简单的用于学习的学校管理系统,可以根据需求进行增强,以开发一个完美的高级系统。下载链接在教程的末尾,可以下载带有数据库表格的完整项目。

另外,请阅读。

所以,让我们开始用PHP和MySQL实现学校管理系统。在我们开始之前,请看一下这个例子的文件结构。

  • index.php
  • School.php。一个用于保存学校方法的类。
  • dashboard.php
  • students.php
  • teacher.php
  • classes.php
  • subjects.php
  • sections.php
  • attendance.php

步骤1:创建MySQL数据库表

首先我们将MySQL数据库表sms_usersms_teachersms_students、sms**_classes**、sms_subjectssms_sectionsms_attendance。所有的表结构和数据都可以在项目下载的压缩文件中找到。

第二步:创建用户登录

index.php文件中,我们将创建登录表单,实现管理员登录,只允许登录的用户访问。

<div class="container">		
	<div class="col-md-6">                    
		<div class="panel panel-info">
			<div class="panel-heading" style="background:#000;color:white;">
				<div class="panel-title">Admin Login</div>                        
			</div> 
			<div style="padding-top:30px" class="panel-body" >
				<?php if ($errorMessage != '') { ?>
					<div id="login-alert" class="alert alert-danger col-sm-12"><?php echo $errorMessage; ?></div>                            
				<?php } ?>
				<form id="loginform" class="form-horizontal" role="form" method="POST" action="">                                    
					<div style="margin-bottom: 25px" class="input-group">
						<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
						<input type="text" class="form-control" id="email" name="email" placeholder="email" required>                                        
					</div>                                
					<div style="margin-bottom: 25px" class="input-group">
						<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
						<input type="password" class="form-control" id="password" name="password" placeholder="password" required>
					</div>
					<div style="margin-top:10px" class="form-group">                               
						<div class="col-sm-12 controls">
						  <input type="submit" name="login" value="Login" class="btn btn-success">						  
						</div>						
					</div>	
					<div style="margin-top:10px" class="form-group">                               
						<div class="col-sm-12 controls">
						Admin: admin@webdamn.com<br>
						password:123	<br><br>									
						</div>						
					</div>	
				</form>   
			</div>                     
		</div>  
	</div>

我们将通过调用ClassSchool.php中的方法**adminLogin()**来实现用户在登录表格中的登录。

include('class/School.php');
$school = new School();
$school->adminLogin();

在classSchool.php中,我们将实现方法adminLogin()

public function adminLogin(){		
	$errorMessage = '';
	if(!empty($_POST["login"]) && $_POST["email"]!=''&& $_POST["password"]!='') {	
		$email = $_POST['email'];
		$password = $_POST['password'];
		$sqlQuery = "SELECT * FROM ".$this->userTable." 
			WHERE email='".$email."' AND password='".md5($password)."' AND status = 'active' AND type = 'administrator'";
		$resultSet = mysqli_query($this->dbConnect, $sqlQuery) or die("error".mysql_error());
		$isValidLogin = mysqli_num_rows($resultSet);	
		if($isValidLogin){
			$userDetails = mysqli_fetch_assoc($resultSet);
			$_SESSION["adminUserid"] = $userDetails['id'];
			$_SESSION["admin"] = $userDetails['first_name']." ".$userDetails['last_name'];
			header("location: dashboard.php"); 		
		} else {		
			$errorMessage = "Invalid login!";		 
		}
	} else if(!empty($_POST["login"])){
		$errorMessage = "Enter Both user and password!";	
	}
	return $errorMessage; 		
}	

第三步:管理教师部分

teacher.php文件中,我们将创建设计来添加新的教师信息,编辑和显示教师列表。

<div class="content">
	<div class="container-fluid">
		<div>   
			<a href="#"><strong><span class="ti-crown"></span> Teachers Section</strong></a>
			<hr>		
			<div class="panel-heading">
				<div class="row">
					<div class="col-md-10">
						<h3 class="panel-title"></h3>
					</div>
					<div class="col-md-2" align="right">
						<button type="button" name="add" id="addTeacher" class="btn btn-success btn-xs">Add New Teacher</button>
					</div>
				</div>
			</div>
			<table id="teacherList" class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>ID</th>
						<th>Name</th>	
						<th>Assigned Subjects</th>	
						<th>Class</th>	
						<th>Sections</th>								
						<th></th>
						<th></th>							
					</tr>
				</thead>
			</table>
			
		</div>			
	</div>		
</div>	

我们将调用学校类方法addTeacher(),updateTeacher和**listTeacher()**来处理教师功能。

public function listTeacher(){		
	$sqlQuery = "SELECT t.teacher_id, t.teacher, s.subject, c.name, se.section			
		FROM ".$this-<teacherTable." as t 
		LEFT JOIN ".$this-<subjectsTable." as s ON t.subject_id = s.subject_id
		LEFT JOIN ".$this-<classesTable." as c ON t.teacher_id = c.teacher_id
		LEFT JOIN ".$this-<sectionsTable." as se ON c.section = se.section_id ";
	if(!empty($_POST["search"]["value"])){
		$sqlQuery .= ' WHERE (t.teacher_id LIKE "%'.$_POST["search"]["value"].'%" ';
		$sqlQuery .= ' OR t.teacher LIKE "%'.$_POST["search"]["value"].'%" ';					
	}
	if(!empty($_POST["order"])){
		$sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
	} else {
		$sqlQuery .= 'ORDER BY t.teacher_id DESC ';
	}
	if($_POST["length"] != -1){
		$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
	}	
	$result = mysqli_query($this-<dbConnect, $sqlQuery);
	$numRows = mysqli_num_rows($result);
	$teacherData = array();	
	while( $teacher = mysqli_fetch_assoc($result) ) {		
		$teacherRows = array();			
		$teacherRows[] = $teacher['teacher_id'];
		$teacherRows[] = $teacher['teacher'];
		$teacherRows[] = $teacher['subject'];
		$teacherRows[] = $teacher['name'];	
		$teacherRows[] = $teacher['section'];				
		$teacherRows[] = '>button type="button" name="update" id="'.$teacher["teacher_id"].'" class="btn btn-warning btn-xs update"<Update>/button<';
		$teacherRows[] = '>button type="button" name="delete" id="'.$teacher["teacher_id"].'" class="btn btn-danger btn-xs delete" <Delete>/button<';
		$teacherData[] = $teacherRows;
	}
	$output = array(
		"draw"				=<	intval($_POST["draw"]),
		"recordsTotal"  	=<  $numRows,
		"recordsFiltered" 	=< 	$numRows,
		"data"    			=< 	$teacherData
	);
	echo json_encode($output);
}

第四步:管理学生部分

students.php文件中,我们将设计显示学生列表,保存新的学生录取,更新和删除学生的详细信息。

<div class="content">
	<div class="container-fluid">
		<div>   
			<a href="#"><strong><span class="ti-crown"></span> Student Section</strong></a>
			<hr>		
			<div class="panel-heading">
				<div class="row">
					<div class="col-md-10">
						<h3 class="panel-title"></h3>
					</div>
					<div class="col-md-2" align="right">
						<button type="button" name="add" id="addStudent" class="btn btn-success btn-xs">Student Admission</button>
					</div>
				</div>
			</div>
			<table id="studentList" class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>ID</th>
						<th>Reg No</th>
						<th>Roll No</th>	
						<th>Name</th>
						<th>Photo</th>	
						<th>Class</th>
						<th>Section</th>				
							
						<th></th>
						<th></th>							
					</tr>
				</thead>
			</table>
			
		</div>			
	</div>		
</div>

我们将调用学校类方法addStudent(),updateStudent,deleteStudent和**listStudent()**来处理教师功能。

public function listStudent(){		
	$sqlQuery = "SELECT s.id, s.name, s.photo, s.gender, s.dob, s.mobile, s.email, s.current_address, s.father_name, s.mother_name,s.admission_no, s.roll_no, s.admission_date, s.academic_year, c.name as class, se.section 
		FROM ".$this->studentTable." as s
		LEFT JOIN ".$this->classesTable." as c ON s.class = c.id
		LEFT JOIN ".$this->sectionsTable." as se ON s.section = se.section_id ";
	if(!empty($_POST["search"]["value"])){
		$sqlQuery .= ' WHERE (s.id LIKE "%'.$_POST["search"]["value"].'%" ';
		$sqlQuery .= ' OR s.name LIKE "%'.$_POST["search"]["value"].'%" ';
		$sqlQuery .= ' OR s.gender LIKE "%'.$_POST["search"]["value"].'%" ';		
		$sqlQuery .= ' OR s.mobile LIKE "%'.$_POST["search"]["value"].'%" ';		
		$sqlQuery .= ' OR s.admission_no LIKE "%'.$_POST["search"]["value"].'%" ';	
		$sqlQuery .= ' OR s.roll_no LIKE "%'.$_POST["search"]["value"].'%" ';			
	}
	if(!empty($_POST["order"])){
		$sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
	} else {
		$sqlQuery .= 'ORDER BY s.id DESC ';
	}
	if($_POST["length"] != -1){
		$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
	}	
	$result = mysqli_query($this->dbConnect, $sqlQuery);
	$numRows = mysqli_num_rows($result);
	$studentData = array();	
	while( $student = mysqli_fetch_assoc($result) ) {		
		$studentRows = array();			
		$studentRows[] = $student['id'];
		$studentRows[] = $student['admission_no'];
		$studentRows[] = $student['roll_no'];
		$studentRows[] = $student['name'];	
		$studentRows[] = "<img width='40' height='40' src='upload/".$student['photo']."'>";
		$studentRows[] = $student['class'];
		$studentRows[] = $student['section'];		
		$studentRows[] = '<button type="button" name="update" id="'.$student["id"].'" class="btn btn-warning btn-xs update">Update</button>';
		$studentRows[] = '<button type="button" name="delete" id="'.$student["id"].'" class="btn btn-danger btn-xs delete" >Delete</button>';
		$studentData[] = $studentRows;
	}
	$output = array(
		"draw"				=>	intval($_POST["draw"]),
		"recordsTotal"  	=>  $numRows,
		"recordsFiltered" 	=> 	$numRows,
		"data"    			=> 	$studentData
	);
	echo json_encode($output);
}

第五步:管理班级部分

classes.php文件中,我们将设计HTML来处理班级功能,如创建班级、更新班级、删除班级和列出班级。

<div class="content">
	<div class="container-fluid">
		<div>   
			<a href="#"><strong><span class="ti-crown"></span> Classes Section</strong></a>
			<hr>		
			<div class="panel-heading">
				<div class="row">
					<div class="col-md-10">
						<h3 class="panel-title"></h3>
					</div>
					<div class="col-md-2" align="right">
						<button type="button" name="add" id="addClass" class="btn btn-success btn-xs">Add New Class</button>
					</div>
				</div>
			</div>
			<table id="classList" class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>ID</th>
						<th>Name</th>	
						<th>Sections</th>
						<th>Class Teacher</th>							
						<th></th>
						<th></th>							
					</tr>
				</thead>
			</table>
			
		</div>			
	</div>		
</div>	

我们将调用学校班级方法addClass(),updateClass,deleteClass和**listClasses()**来处理班级功能。

public function listClasses(){		
	$sqlQuery = "SELECT c.id, c.name, s.section, t.teacher 
		FROM ".$this->classesTable." as c
		LEFT JOIN ".$this->sectionsTable." as s ON c.section = s.section_id
		LEFT JOIN ".$this->teacherTable." as t ON c.teacher_id = t.teacher_id ";
	if(!empty($_POST["search"]["value"])){
		$sqlQuery .= ' WHERE (c.id LIKE "%'.$_POST["search"]["value"].'%" ';
		$sqlQuery .= ' OR c.name LIKE "%'.$_POST["search"]["value"].'%" ';	
		$sqlQuery .= ' OR s.section LIKE "%'.$_POST["search"]["value"].'%" ';
		$sqlQuery .= ' OR t.teacher LIKE "%'.$_POST["search"]["value"].'%" ';				
	}
	if(!empty($_POST["order"])){
		$sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
	} else {
		$sqlQuery .= 'ORDER BY c.id DESC ';
	}
	if($_POST["length"] != -1){
		$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
	}	
	$result = mysqli_query($this->dbConnect, $sqlQuery);
	$numRows = mysqli_num_rows($result);
	$classesData = array();	
	while( $classes = mysqli_fetch_assoc($result) ) {		
		$classesRows = array();			
		$classesRows[] = $classes['id'];
		$classesRows[] = $classes['name'];	
		$classesRows[] = $classes['section'];	
		$classesRows[] = $classes['teacher'];	
		$classesRows[] = '<button type="button" name="update" id="'.$classes["id"].'" class="btn btn-warning btn-xs update">Update</button>';
		$classesRows[] = '<button type="button" name="delete" id="'.$classes["id"].'" class="btn btn-danger btn-xs delete" >Delete</button>';
		$classesData[] = $classesRows;
	}
	$output = array(
		"draw"				=>	intval($_POST["draw"]),
		"recordsTotal"  	=>  $numRows,
		"recordsFiltered" 	=> 	$numRows,
		"data"    			=> 	$classesData
	);
	echo json_encode($output);
}

第六步:管理科目部分

subjects.php文件中,我们将设计页面来处理添加新科目、更新、删除和列表科目的功能。

<div class="content">
	<div class="container-fluid">
		<div>   
			<a href="#"><strong><span class="ti-crown"></span> Subjects Section</strong></a>
			<hr>		
			<div class="panel-heading">
				<div class="row">
					<div class="col-md-10">
						<h3 class="panel-title"></h3>
					</div>
					<div class="col-md-2" align="right">
						<button type="button" name="add" id="addSubject" class="btn btn-success btn-xs">Add New Subject</button>
					</div>
				</div>
			</div>
			<table id="subjectList" class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>ID</th>
						<th>Subject</th>	
						<th>Code</th>
						<th>Subject Type</th>							
						<th></th>
						<th></th>							
					</tr>
				</thead>
			</table>
			
		</div>			
	</div>		
</div>	

我们将调用学校类方法addSubject(),updateSubject,deleteSubject和**listSubject()**来处理科目功能。

public function listSubject(){		
	$sqlQuery = "SELECT subject_id, subject, type, code 
		FROM ".$this->subjectsTable." ";
	if(!empty($_POST["search"]["value"])){
		$sqlQuery .= ' WHERE (subject_id LIKE "%'.$_POST["search"]["value"].'%" ';
		$sqlQuery .= ' OR subject LIKE "%'.$_POST["search"]["value"].'%" ';	
		$sqlQuery .= ' OR type LIKE "%'.$_POST["search"]["value"].'%" ';	
		$sqlQuery .= ' OR code LIKE "%'.$_POST["search"]["value"].'%" ';				
	}
	if(!empty($_POST["order"])){
		$sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
	} else {
		$sqlQuery .= 'ORDER BY subject_id DESC ';
	}
	if($_POST["length"] != -1){
		$sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
	}	
	$result = mysqli_query($this->dbConnect, $sqlQuery);
	$numRows = mysqli_num_rows($result);
	$subjectData = array();	
	while( $subject = mysqli_fetch_assoc($result) ) {		
		$subjectRows = array();			
		$subjectRows[] = $subject['subject_id'];
		$subjectRows[] = $subject['subject'];	
		$subjectRows[] = $subject['code'];	
		$subjectRows[] = $subject['type'];				
		$subjectRows[] = '<button type="button" name="update" id="'.$subject["subject_id"].'" class="btn btn-warning btn-xs update">Update</button>';
		$subjectRows[] = '<button type="button" name="delete" id="'.$subject["subject_id"].'" class="btn btn-danger btn-xs delete" >Delete</button>';
		$subjectData[] = $subjectRows;
	}
	$output = array(
		"draw"				=>	intval($_POST["draw"]),
		"recordsTotal"  	=>  $numRows,
		"recordsFiltered" 	=> 	$numRows,
		"data"    			=> 	$subjectData
	);
	echo json_encode($output);
}

第七步:管理学生考勤部分

attend.php文件中,我们将设计HTML来搜索班级和章节的学生出勤情况和列表。我们还将创建学生考勤表来处理学生考勤功能。

<div class="content">
	<div class="container-fluid">
		<strong>Student Attendance Section</strong>
		<div class="row">
			<div class="col-md-12">
				<div class="box box-primary">
					<div class="box-header with-border">
						<h3 class="box-title"><i class="fa fa-search"></i> Select Criteria</h3>
					</div>
					<form id="form1" action="" method="post" accept-charset="utf-8">
						<div class="box-body">						
							<div class="row">
								<div class="col-md-4">
									<div class="form-group">
										<label for="exampleInputEmail1">Class</label><small class="req"> *</small>
										<select id="classid" name="classid" class="form-control" required>
											<option value="">Select</option>
											<?php echo $school->classList(); ?>												
										</select>
										<span class="text-danger"></span>
									</div>
								</div>
								<div class="col-md-4">
									<div class="form-group">
										<label for="exampleInputEmail1">Section</label><small class="req"> *</small>
											<select name="sectionid" id="sectionid" class="form-control" required>
												<option value="">Select</option>
												<?php echo $school->getSectionList(); ?>
											</select>
										<span class="text-danger"></span>
									</div>
								</div> 									
							</div>
						</div>
						<div class="box-footer">
							<button type="button" id="search" name="search" value="search" style="margin-bottom:10px;" class="btn btn-primary btn-sm  checkbox-toggle"><i class="fa fa-search"></i> Search</button> <br>
						</div>
					</form>
				</div>
			</div>
		</div>
		<div class="row">					
			<form id="attendanceForm" method="post">					
				<div style="color:red;margin-top:20px;" class="hidden" id="message"></div>
				<button type="submit" id="saveAttendance" name="saveAttendance" value="Save Attendance" style="margin-bottom:10px;" class="btn btn-primary btn-sm  pull-right checkbox-toggle hidden"><i class="fa fa-save"></i> Save Attendance</button> <table id="studentList" class="table table-bordered table-striped hidden">
					<thead>
						<tr>
							<th>#</th>
							<th>Reg No</th>
							<th>Roll No</th>	
							<th>Name</th>
							<th>Attendance</th>													
						</tr>
					</thead>
				</table>
				<input type="hidden" name="action" id="action" value="updateAttendance" />
				<input type="hidden" name="att_classid" id="att_classid" value="" />
				<input type="hidden" name="att_sectionid" id="att_sectionid" value="" />
			</form>
		</div>					
</div>	

我们将实现班级学生查询功能来执行学生考勤。

$('#search').click(function(){
	$('#studentList').removeClass('hidden');
	$('#saveAttendance').removeClass('hidden');
	if ($.fn.DataTable.isDataTable("#studentList")) {
		$('#studentList').DataTable().clear().destroy();
	}
	var classid = $('#classid').val();
	var sectionid = $('#sectionid').val();
	if(classid && sectionid) {
		$.ajax({
			url:"action.php",
			method:"POST",
			data:{classid:classid, sectionid:sectionid, action:"attendanceStatus"},
			success:function(data) {					
				$('#message').text(data).removeClass('hidden');	
			}
		})
		$('#studentList').DataTable({
			"lengthChange": false,
			"processing":true,
			"serverSide":true,
			"order":[],
			"ajax":{
				url:"action.php",
				type:"POST",				
				data:{classid:classid, sectionid:sectionid, action:'getStudents'},
				dataType:"json"
			},
			"columnDefs":[
				{
					"targets":[0],
					"orderable":false,
				},
			],
			"pageLength": 10
		});				
	}
});	

我们还将实现学生考勤功能,处理表单提交。

$("#attendanceForm").submit(function(e) {		
	var formData = $(this).serialize();
	$.ajax({
		url:"action.php",
		method:"POST",
		data:formData,
		success:function(data){				
			$('#message').text(data).removeClass('hidden');				
		}
	});
	return false;
});	

我们将通过调用ClassSchool.php中的**updateAttendance()**方法来处理学生考勤的更新功能。

public function updateAttendance(){	
	$attendanceYear = date('Y'); 
	$attendanceMonth = date('m'); 
	$attendanceDay = date('d'); 
	$attendanceDate = $attendanceYear."/".$attendanceMonth."/".$attendanceDay;		
	$sqlQuery = "SELECT * FROM ".$this->attendanceTable." 
		WHERE class_id = '".$_POST["att_classid"]."' AND section_id = '".$_POST["att_sectionid"]."' AND attendance_date = '".$attendanceDate."'";	
	$result = mysqli_query($this->dbConnect, $sqlQuery);	
	$attendanceDone = mysqli_num_rows($result);
	if($attendanceDone) {
		foreach($_POST as $key => $value) {				
			if (strpos($key, "attendencetype_") !== false) {
				$student_id = str_replace("attendencetype_","", $key);
				$attendanceStatus = $value;					
				if($student_id) {
					$updateQuery = "UPDATE ".$this->attendanceTable." SET attendance_status = '".$attendanceStatus."'
					WHERE student_id = '".$student_id."' AND class_id = '".$_POST["att_classid"]."' AND section_id = '".$_POST["att_sectionid"]."' AND attendance_date = '".$attendanceDate."'";
					mysqli_query($this->dbConnect, $updateQuery);
				}
			}				
		}	
		echo "Attendance updated successfully!";			
	} else {
		foreach($_POST as $key => $value) {				
			if (strpos($key, "attendencetype_") !== false) {
				$student_id = str_replace("attendencetype_","", $key);
				$attendanceStatus = $value;					
				if($student_id) {
					$insertQuery = "INSERT INTO ".$this->attendanceTable."(student_id, class_id, section_id, attendance_status, attendance_date) 
					VALUES ('".$student_id."', '".$_POST["att_classid"]."', '".$_POST["att_sectionid"]."', '".$attendanceStatus."', '".$attendanceDate."')";
					mysqli_query($this->dbConnect, $insertQuery);
				}
			}
			
		}
		echo "Attendance save successfully!";
	}	
}

你可能也喜欢。

你可以从演示链接中查看实时演示,也可以从下面的下载链接中下载脚本。
演示 下载

The postSchool Management System with PHP & MySQLfirst appeared onWD.