0% found this document useful (0 votes)
13 views4 pages

Educational System

Uploaded by

selinasacc190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Educational System

Uploaded by

selinasacc190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE DATABASE educational_institute;

USE educational_institute;

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role ENUM('student', 'admin') DEFAULT 'student'
);

CREATE TABLE courses (


id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL,
prerequisites VARCHAR(100)
);

CREATE TABLE enrollments (


id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
course_id INT,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (course_id) REFERENCES courses(id)
);

register.php-
<?php
$conn = new mysqli("localhost", "root", "", "educational_institute");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {


$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);

$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");


$stmt->bind_param("ss", $username, $password);

if ($stmt->execute()) {
echo "Registration successful.";
} else {
echo "Error: Could not register user.";
}
}
?>

<h2>Register</h2>
<form method="post">
<label>Username: <input type="text" name="username" required></label><br>
<label>Password: <input type="password" name="password" required></label><br>
<button type="submit">Register</button>
</form>

login.php-
<?php
session_start();
$conn = new mysqli("localhost", "root", "", "educational_institute");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {


$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $conn->prepare("SELECT id, password FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user_id, $hashed_password);
$stmt->fetch();

if ($stmt->num_rows > 0 && password_verify($password, $hashed_password)) {


$_SESSION['user_id'] = $user_id;
header("Location: dashboard.php");
} else {
echo "Invalid username or password.";
}
}
?>

<h2>Login</h2>
<form method="post">
<label>Username: <input type="text" name="username" required></label><br>
<label>Password: <input type="password" name="password" required></label><br>
<button type="submit">Login</button>
</form>

dashboard-
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}

$conn = new mysqli("localhost", "root", "", "educational_institute");


$user_id = $_SESSION['user_id'];

// Fetch available courses


$courses = $conn->query("SELECT * FROM courses");

?>

<h2>Available Courses</h2>
<form method="post" action="register_course.php">
<label>Select Course:
<select name="course_id">
<?php while ($course = $courses->fetch_assoc()): ?>
<option value="<?= $course['id'] ?>"><?= $course['course_name'] ?>
(Prerequisite: <?= $course['prerequisites'] ?>)</option>
<?php endwhile; ?>
</select>
</label><br>
<button type="submit">Register for Course</button>
</form>

<a href="logout.php">Logout</a>

register course-
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}

$conn = new mysqli("localhost", "root", "", "educational_institute");

$user_id = $_SESSION['user_id'];
$course_id = $_POST['course_id'];

// Fetch course details


$result = $conn->query("SELECT * FROM courses WHERE id = $course_id");
$course = $result->fetch_assoc();

// Fetch student enrollments (checking prerequisites)


$prerequisite = $course['prerequisites'];
$enrollments = $conn->query("SELECT c.course_name
FROM courses c
JOIN enrollments e ON c.id = e.course_id
WHERE e.user_id = $user_id AND c.course_name =
'$prerequisite'");

if ($prerequisite && $enrollments->num_rows == 0) {


echo "You must complete the prerequisite course '$prerequisite' before
enrolling in this course.";
} else {
// Register the student for the course
$stmt = $conn->prepare("INSERT INTO enrollments (user_id, course_id) VALUES (?,
?)");
$stmt->bind_param("ii", $user_id, $course_id);
if ($stmt->execute()) {
echo "You have been successfully registered for the course.";
} else {
echo "Error: Could not register for the course.";
}
}

change password-
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}

$conn = new mysqli("localhost", "root", "", "educational_institute");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {


$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];

// Fetch current password


$stmt = $conn->prepare("SELECT password FROM users WHERE id = ?");
$stmt->bind_param("i", $_SESSION['user_id']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($hashed_password);
$stmt->fetch();

// Verify current password


if (password_verify($current_password, $hashed_password)) {
$new_hashed_password = password_hash($new_password, PASSWORD_BCRYPT);

// Update password
$stmt = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");
$stmt->bind_param("si", $new_hashed_password, $_SESSION['user_id']);

if ($stmt->execute()) {
echo "Password updated successfully.";
} else {
echo "Error: Could not update password.";
}
} else {
echo "Current password is incorrect.";
}
}
?>

<h2>Change Password</h2>
<form method="post">
<label>Current Password: <input type="password" name="current_password"
required></label><br>
<label>New Password: <input type="password" name="new_password"
required></label><br>
<button type="submit">Change Password</button>
</form>

logout-
<?php
session_start();
session_destroy();
header("Location: login.php");
exit();

You might also like