Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Student Enrollment Project

The document contains a Flask application for managing student enrollment in courses, including HTML templates for available courses and enrollment pages, along with CSS for styling. It connects to a MySQL database with a schema defining students, courses, and enrollments. The application allows users to enroll students, view available courses, and displays a list of enrolled students in a table format.

Uploaded by

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

Student Enrollment Project

The document contains a Flask application for managing student enrollment in courses, including HTML templates for available courses and enrollment pages, along with CSS for styling. It connects to a MySQL database with a schema defining students, courses, and enrollments. The application allows users to enroll students, view available courses, and displays a list of enrolled students in a table format.

Uploaded by

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

CSS Code

form {
display: flex;
flex-direction: column;
max-width: 400px;
margin-bottom: 20px;
}
input, select, button {
padding: 10px;
margin: 5px 0;
font-size: 1em;
}
@media (max-width: 600px) {
table, th, td {
font-size: 0.9em;
}
}

HTML - Available Courses Page

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Available Courses</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>Available Courses</h1>
<ul>
{% for course in course_list %}
<li>{{ course }}</li>
{% endfor %}
</ul>
<a href="/"><- Back to Enrollment</a>
</body>
</html>

HTML - Enrollment Page

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Enrollment</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>Enroll a Student</h1>
<form action="/enroll" method="post">
<input type="text" name="name" placeholder="Full Name" required>
<input type="email" name="email" placeholder="Email" required>
<button type="submit">Enroll</button>
</form>
<a href="/courses">
<button style="margin-top: 20px;">View Available Courses</button>
</a>
<h2>Enrollment List</h2>
<table>
<thead>
<tr>
<th>Student Name</th>
<th>Course</th>
<th>Instructor</th>
</tr>
</thead>
<tbody>
{% for e in enrollments %}
<tr>
<td>{{ e.name }}</td>
<td>{{ e.course_name }}</td>
<td>{{ e.instructor }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

Flask Application Code

from flask import Flask, render_template, request, redirect


import mysql.connector

app = Flask(__name__)

conn = mysql.connector.connect(
host="localhost",
user="root",
password="Shashank@123",
database="enrollment_db"
)
cursor = conn.cursor(dictionary=True)

@app.route('/')
def index():
cursor.execute("""
SELECT s.name, c.course_name, c.instructor
FROM enrollments e
JOIN students s ON e.student_id = s.student_id
JOIN courses c ON e.course_id = c.course_id
""")
enrollments = cursor.fetchall()
return render_template('index.html', enrollments=enrollments)

@app.route('/enroll', methods=['POST'])
def enroll():
name = request.form['name']
email = request.form['email']
default_course = 'Python Programming'
cursor.execute("SELECT course_id FROM courses WHERE course_name = %s",
(default_course,))
course = cursor.fetchone()
if not course:
cursor.execute("INSERT INTO courses (course_name, instructor) VALUES (%s, %s)",
(default_course, "System Assigned"))
conn.commit()
course_id = cursor.lastrowid
else:
course_id = course['course_id']

cursor.execute("SELECT student_id FROM students WHERE email = %s", (email,))


student = cursor.fetchone()
if student:
student_id = student['student_id']
else:
cursor.execute("INSERT INTO students (name, email) VALUES (%s, %s)", (name,
email))
conn.commit()
student_id = cursor.lastrowid

cursor.execute("INSERT INTO enrollments (student_id, course_id) VALUES (%s, %s)",


(student_id, course_id))
conn.commit()
return redirect('/')

@app.route('/courses')
def courses():
course_list = ['Math', 'Physics', 'Python Programming', 'Web Development', 'Data
Science']
return render_template('courses.html', course_list=course_list)

if __name__ == '__main__':
app.run(debug=True)

SQL - Database Schema

CREATE DATABASE enrollment_db;


USE enrollment_db;

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);

CREATE TABLE courses (


course_id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100),
instructor VARCHAR(100)
);

CREATE TABLE enrollments (


enrollment_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

You might also like