diff --git a/admin/add_course.php b/admin/add_course.php new file mode 100644 index 0000000..8065cd2 --- /dev/null +++ b/admin/add_course.php @@ -0,0 +1,41 @@ +prepare("INSERT INTO courses (name, description) VALUES (?, ?)"); + $stmt->execute([$name, $description]); + + header("Location: courses.php"); + exit; +} + +require_once 'partials/header.php'; +?> + +
+

Add Course

+
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/admin/add_student.php b/admin/add_student.php new file mode 100644 index 0000000..d5461d1 --- /dev/null +++ b/admin/add_student.php @@ -0,0 +1,41 @@ +prepare("INSERT INTO students (username, password) VALUES (?, ?)"); + $stmt->execute([$username, $password]); + + header("Location: students.php"); + exit; +} + +require_once 'partials/header.php'; +?> + +
+

Add Student

+
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/admin/courses.php b/admin/courses.php new file mode 100644 index 0000000..7b12fa0 --- /dev/null +++ b/admin/courses.php @@ -0,0 +1,46 @@ +query("SELECT * FROM courses"); +$courses = $stmt->fetchAll(); + +?> + +
+

Manage Courses

+ Add Course + + + + + + + + + + + + + + + + + + + +
IDNameDescriptionActions
+ Edit + Delete +
+
+ + diff --git a/admin/delete_course.php b/admin/delete_course.php new file mode 100644 index 0000000..141bfa3 --- /dev/null +++ b/admin/delete_course.php @@ -0,0 +1,33 @@ +prepare("SELECT id FROM enrollments WHERE course_id = ?"); +$stmt->execute([$course_id]); +$enrollments = $stmt->fetchAll(); + +foreach ($enrollments as $enrollment) { + $stmt = $pdo->prepare("DELETE FROM grades WHERE enrollment_id = ?"); + $stmt->execute([$enrollment['id']]); +} + +$stmt = $pdo->prepare("DELETE FROM enrollments WHERE course_id = ?"); +$stmt->execute([$course_id]); + +// Now, delete the course +$stmt = $pdo->prepare("DELETE FROM courses WHERE id = ?"); +$stmt->execute([$course_id]); + +header("Location: courses.php"); +exit; diff --git a/admin/delete_student.php b/admin/delete_student.php new file mode 100644 index 0000000..3a10585 --- /dev/null +++ b/admin/delete_student.php @@ -0,0 +1,33 @@ +prepare("SELECT id FROM enrollments WHERE student_id = ?"); +$stmt->execute([$student_id]); +$enrollments = $stmt->fetchAll(); + +foreach ($enrollments as $enrollment) { + $stmt = $pdo->prepare("DELETE FROM grades WHERE enrollment_id = ?"); + $stmt->execute([$enrollment['id']]); +} + +$stmt = $pdo->prepare("DELETE FROM enrollments WHERE student_id = ?"); +$stmt->execute([$student_id]); + +// Now, delete the student +$stmt = $pdo->prepare("DELETE FROM students WHERE id = ?"); +$stmt->execute([$student_id]); + +header("Location: students.php"); +exit; diff --git a/admin/edit_course.php b/admin/edit_course.php new file mode 100644 index 0000000..edf5ec2 --- /dev/null +++ b/admin/edit_course.php @@ -0,0 +1,47 @@ +prepare("UPDATE courses SET name = ?, description = ? WHERE id = ?"); + $stmt->execute([$name, $description, $course_id]); + + header("Location: courses.php"); + exit; +} + +$stmt = $pdo->prepare("SELECT * FROM courses WHERE id = ?"); +$stmt->execute([$course_id]); +$course = $stmt->fetch(); + +require_once 'partials/header.php'; +?> + +
+

Edit Course

+
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/admin/edit_student.php b/admin/edit_student.php new file mode 100644 index 0000000..43f2a3c --- /dev/null +++ b/admin/edit_student.php @@ -0,0 +1,58 @@ +prepare($sql); + $stmt->execute($params); + + header("Location: students.php"); + exit; +} + +$stmt = $pdo->prepare("SELECT * FROM students WHERE id = ?"); +$stmt->execute([$student_id]); +$student = $stmt->fetch(); + +require_once 'partials/header.php'; +?> + +
+

Edit Student

+
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/admin/enrollments.php b/admin/enrollments.php new file mode 100644 index 0000000..1eca2e1 --- /dev/null +++ b/admin/enrollments.php @@ -0,0 +1,106 @@ +prepare("SELECT id FROM enrollments WHERE student_id = ? AND course_id = ?"); + $stmt->execute([$student_id, $course_id]); + if (!$stmt->fetch()) { + $stmt = $pdo->prepare("INSERT INTO enrollments (student_id, course_id) VALUES (?, ?)"); + $stmt->execute([$student_id, $course_id]); + } + + header("Location: enrollments.php"); + exit; +} + +// Fetch all students and courses for dropdowns +$students_stmt = $pdo->query("SELECT * FROM students"); +$students = $students_stmt->fetchAll(); + +$courses_stmt = $pdo->query("SELECT * FROM courses"); +$courses = $courses_stmt->fetchAll(); + +// Fetch all enrollments with student and course names +$enrollments_stmt = $pdo->query(" + SELECT e.id, s.username, c.name as course_name + FROM enrollments e + JOIN students s ON e.student_id = s.id + JOIN courses c ON e.course_id = c.id + ORDER BY s.username, c.name +"); +$enrollments = $enrollments_stmt->fetchAll(); + +require_once 'partials/header.php'; +?> + +
+

Manage Enrollments

+ +
+
Enroll Student in Course
+
+
+
+
+ + +
+
+ + +
+
+ +
+
+
+
+
+ +

Current Enrollments

+ + + + + + + + + + + + + + + + + +
StudentCourseActions
+ Unenroll +
+
+ + diff --git a/admin/grades.php b/admin/grades.php new file mode 100644 index 0000000..22aba2d --- /dev/null +++ b/admin/grades.php @@ -0,0 +1,84 @@ +prepare("SELECT id FROM grades WHERE enrollment_id = ?"); + $stmt->execute([$enrollment_id]); + $existing_grade = $stmt->fetch(); + + if ($existing_grade) { + // Update existing grade + $stmt = $pdo->prepare("UPDATE grades SET grade = ? WHERE id = ?"); + $stmt->execute([$grade, $existing_grade['id']]); + } else { + // Insert new grade + $stmt = $pdo->prepare("INSERT INTO grades (enrollment_id, grade) VALUES (?, ?)"); + $stmt->execute([$enrollment_id, $grade]); + } + + header("Location: grades.php"); + exit; +} + + +// Fetch all enrollments with student, course, and grade information +$enrollments_stmt = $pdo->query(" + SELECT e.id, s.username, c.name as course_name, g.grade + FROM enrollments e + JOIN students s ON e.student_id = s.id + JOIN courses c ON e.course_id = c.id + LEFT JOIN grades g ON e.id = g.enrollment_id + ORDER BY s.username, c.name +"); +$enrollments = $enrollments_stmt->fetchAll(); + +require_once 'partials/header.php'; +?> + +
+

Manage Grades

+ + + + + + + + + + + + + + + + + + +
StudentCourseGradeActions
+
+ +
+ +
+
+ +
+
+
+
+ + diff --git a/admin/index.php b/admin/index.php new file mode 100644 index 0000000..494a1c1 --- /dev/null +++ b/admin/index.php @@ -0,0 +1,18 @@ + + +
+

Admin Dashboard

+

Welcome, !

+

This is the admin dashboard. From here you can manage students, courses, enrollments, and grades.

+
+ + diff --git a/admin/login.php b/admin/login.php new file mode 100644 index 0000000..e0ca5db --- /dev/null +++ b/admin/login.php @@ -0,0 +1,64 @@ +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_id'] = $admin['id']; + $_SESSION['admin_username'] = $admin['username']; + header("Location: index.php"); + exit; + } else { + $error = "Invalid credentials"; + } +} +?> + + + + + + + Admin Login + + + +
+
+
+
+
+ Admin Login +
+
+ +
+ +
+
+ + +
+
+ + +
+ +
+
+
+
+
+
+ + + diff --git a/admin/logout.php b/admin/logout.php new file mode 100644 index 0000000..766a593 --- /dev/null +++ b/admin/logout.php @@ -0,0 +1,6 @@ + + + diff --git a/admin/partials/header.php b/admin/partials/header.php new file mode 100644 index 0000000..ffebf4b --- /dev/null +++ b/admin/partials/header.php @@ -0,0 +1,42 @@ + + + + + + Admin Panel + + + + + diff --git a/admin/students.php b/admin/students.php new file mode 100644 index 0000000..8476a40 --- /dev/null +++ b/admin/students.php @@ -0,0 +1,44 @@ +query("SELECT * FROM students"); +$students = $stmt->fetchAll(); + +?> + +
+

Manage Students

+ Add Student + + + + + + + + + + + + + + + + + +
IDUsernameActions
+ Edit + Delete +
+
+ + diff --git a/admin/unenroll.php b/admin/unenroll.php new file mode 100644 index 0000000..9c788e4 --- /dev/null +++ b/admin/unenroll.php @@ -0,0 +1,24 @@ +prepare("DELETE FROM grades WHERE enrollment_id = ?"); +$stmt->execute([$enrollment_id]); + +// Now, delete the enrollment +$stmt = $pdo->prepare("DELETE FROM enrollments WHERE id = ?"); +$stmt->execute([$enrollment_id]); + +header("Location: enrollments.php"); +exit; diff --git a/dashboard.php b/dashboard.php index 8f093a3..c852607 100644 --- a/dashboard.php +++ b/dashboard.php @@ -1,11 +1,32 @@ prepare(" + SELECT c.name, c.description, g.grade + FROM courses c + JOIN enrollments e ON c.id = e.course_id + JOIN students s ON e.student_id = s.id + LEFT JOIN grades g ON e.id = g.enrollment_id + WHERE s.username = ? + "); + $stmt->execute([$_SESSION['username']]); + $courses = $stmt->fetchAll(); +} catch (PDOException $e) { + // Handle database error + error_log($e->getMessage()); +} + ?> @@ -52,8 +73,23 @@ if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {

Enrolled Courses

-

Course information will be displayed here soon.

- + + + +

You are not enrolled in any courses yet.

+
@@ -63,7 +99,8 @@ if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {

Profile

-

Student profile details will appear here.

+

View and edit your profile information.

+ Go to Profile
diff --git a/db/setup_database.php b/db/setup_database.php new file mode 100644 index 0000000..a6486a1 --- /dev/null +++ b/db/setup_database.php @@ -0,0 +1,109 @@ +exec(" + CREATE TABLE IF NOT EXISTS students ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(255) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL + ) + "); + + // Create courses table + $pdo->exec(" + CREATE TABLE IF NOT EXISTS courses ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + description TEXT + ) + "); + + // Create enrollments table + $pdo->exec(" + CREATE TABLE IF NOT EXISTS enrollments ( + id INT AUTO_INCREMENT PRIMARY KEY, + student_id INT NOT NULL, + course_id INT NOT NULL, + FOREIGN KEY (student_id) REFERENCES students(id), + FOREIGN KEY (course_id) REFERENCES courses(id) + ) + "); + + // Add a student + $username = 'student'; + $password = password_hash('password123', PASSWORD_DEFAULT); + + $stmt = $pdo->prepare("SELECT id FROM students WHERE username = ?"); + $stmt->execute([$username]); + $student = $stmt->fetch(); + + if (!$student) { + $stmt = $pdo->prepare("INSERT INTO students (username, password) VALUES (?, ?)"); + $stmt->execute([$username, $password]); + $student_id = $pdo->lastInsertId(); + } else { + $student_id = $student['id']; + } + + + // Add courses + $courses = [ + ['Introduction to PHP', 'Learn the basics of PHP programming.'], + ['Database Management with MySQL', 'Master the art of database management.'], + ['Web Design Fundamentals', 'Understand the principles of modern web design.'], + ]; + + $stmt = $pdo->prepare("INSERT INTO courses (name, description) VALUES (?, ?)"); + foreach ($courses as $course) { + // Check if course exists + $checkStmt = $pdo->prepare("SELECT id FROM courses WHERE name = ?"); + $checkStmt->execute([$course[0]]); + if (!$checkStmt->fetch()) { + $stmt->execute($course); + $course_id = $pdo->lastInsertId(); + + // Enroll student in the course + $enrollStmt = $pdo->prepare("INSERT INTO enrollments (student_id, course_id) VALUES (?, ?)"); + $enrollStmt->execute([$student_id, $course_id]); + } + } + + // Create admins table + $pdo->exec(" + CREATE TABLE IF NOT EXISTS admins ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(255) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL + ) + "); + + // Add an admin + $admin_username = 'admin'; + $admin_password = password_hash('password123', PASSWORD_DEFAULT); + + $stmt = $pdo->prepare("SELECT id FROM admins WHERE username = ?"); + $stmt->execute([$admin_username]); + if (!$stmt->fetch()) { + $stmt = $pdo->prepare("INSERT INTO admins (username, password) VALUES (?, ?)"); + $stmt->execute([$admin_username, $admin_password]); + } + + // Create grades table + $pdo->exec(" + CREATE TABLE IF NOT EXISTS grades ( + id INT AUTO_INCREMENT PRIMARY KEY, + enrollment_id INT NOT NULL, + grade VARCHAR(255), + FOREIGN KEY (enrollment_id) REFERENCES enrollments(id) + ) + "); + + echo "Database setup completed successfully.\n"; + +} catch (PDOException $e) { + die("Database error: " . $e->getMessage()); +} diff --git a/index.php b/index.php index 9312c32..742e7f4 100644 --- a/index.php +++ b/index.php @@ -37,6 +37,7 @@ + diff --git a/login.php b/login.php index c8d6393..bf22c6b 100644 --- a/login.php +++ b/login.php @@ -1,5 +1,6 @@ prepare("SELECT * FROM students WHERE username = ?"); + $stmt->execute([$username]); + $student = $stmt->fetch(); + + if ($student && password_verify($password, $student['password'])) { + $_SESSION['loggedin'] = true; + $_SESSION['username'] = $student['username']; + $_SESSION['student_id'] = $student['id']; + header('Location: dashboard.php'); + exit; + } else { + $error = 'Invalid username or password.'; + } + } catch (PDOException $e) { + $error = 'Database error. Please try again later.'; + error_log($e->getMessage()); + } } } ?> diff --git a/profile.php b/profile.php new file mode 100644 index 0000000..6983fab --- /dev/null +++ b/profile.php @@ -0,0 +1,69 @@ + + + + + + + My Profile - University Management + + + + + + +
+
+
+

My Profile

+
+
+
+
+
+
+

Profile Information

+
+
+

Username:

+ +
+
+
+
+
+ + + +