Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hung1605 committed May 22, 2024
0 parents commit af13e9d
Show file tree
Hide file tree
Showing 15 changed files with 629 additions and 0 deletions.
60 changes: 60 additions & 0 deletions add_student.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header("Location: login.php");
exit();
}

include 'functions/class_functions.php';
include 'templates/header.php';
include 'functions/student_functions.php';

// Retrieve the list of classes from the database
$classes = getAllClasses();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$class = $_POST['class'];
$dob = $_POST['dob'];
$address = $_POST['address'];
$gender = $_POST['gender'];

if (addStudent($name, $class, $dob, $address, $gender)) {
echo "Student added successfully.";
header("Location: all_students.php");
exit();
} else {
echo "Error adding student.";
}
}
?>

<h2>Add Student</h2>

<form method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br>

<label for="class">Class:</label><br>
<select id="class" name="class" required>
<?php while ($row = $classes->fetch_assoc()) { ?>
<option value="<?php echo $row['MaLop']; ?>"><?php echo $row['TenLop']; ?></option>
<?php } ?>
</select><br>

<label for="dob">Date of Birth:</label><br>
<input type="date" id="dob" name="dob" required><br>

<label for="address">Address:</label><br>
<input type="text" id="address" name="address" required><br>

<label for="gender">Gender:</label><br>
<input type="radio" id="male" name="gender" value="Male" required>
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="Female" required>
<label for="female">Female</label><br><br>

<input type="submit" value="Add Student">
</form>

<?php include 'templates/footer.php'; ?>
81 changes: 81 additions & 0 deletions all_students.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header("Location: login.php");
exit();
}

include 'templates/header.php';
include 'functions/student_functions.php';
?>

<!-- Include JavaScript for dynamic search suggestions -->
<script>
document.addEventListener("DOMContentLoaded", function() {
var searchInput = document.getElementById("searchInput");
var suggestionBox = document.getElementById("suggestionBox");

searchInput.addEventListener("input", function() {
var searchQuery = searchInput.value.trim();
if (searchQuery.length > 0) {
fetchSuggestions(searchQuery);
} else {
suggestionBox.innerHTML = "";
}
});

function fetchSuggestions(query) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
suggestionBox.innerHTML = xhr.responseText;
}
};
xhr.open("GET", "functions/search_students.php?search=" + query, true);
xhr.send();
}
});
</script>

<!-- HTML for search input and suggestion box -->
<h2>All Students</h2>

<div>
<input type="text" id="searchInput" placeholder="Search by Name">
<div id="suggestionBox"></div>
</div>

<button onclick="window.location.href='add_student.php'">Add Student</button>

<div id="studentList">
<?php
$students = getAllStudents();
if ($students->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Class</th><th>DOB</th><th>Address</th><th>Actions</th></tr>";
while ($row = $students->fetch_assoc()) {
echo "<tr>
<td>{$row['MaSV']}</td>
<td>{$row['HoTen']}</td>
<td>{$row['MaLop']}</td>
<td>{$row['NgaySinh']}</td>
<td>{$row['DiaChi']}</td>
<td>
<button onclick=\"openPopup('update_student.php?id={$row['MaSV']}')\">Update</button>
<button onclick=\"openPopup('delete_student.php?id={$row['MaSV']}')\">Delete</button>
</td>
</tr>";
}
echo "</table>";
} else {
echo "No students found.";
}
?>
</div>

<script>
function openPopup(url) {
window.open(url, '_self', 'width=600,height=400');
}
</script>

<?php include 'templates/footer.php'; ?>
12 changes: 12 additions & 0 deletions db/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student_manager";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
37 changes: 37 additions & 0 deletions db/ini_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Insert sample data into 'khoa' table
INSERT INTO khoa (MaKhoa, TenKhoa) VALUES
('K01', 'Computer Science'),
('K02', 'Mathematics'),
('K03', 'Physics');

-- Insert sample data into 'nganh' table
INSERT INTO nganh (MaNganh, TenNganh, MaKhoa) VALUES
('N01', 'Software Engineering', 'K01'),
('N02', 'Data Science', 'K01'),
('N03', 'Applied Mathematics', 'K02');

-- Insert sample data into 'lop' table
INSERT INTO lop (MaLop, TenLop, MaNganh, KhoaHoc, HeDT, NamNhapHoc) VALUES
('L01', 'SE01', 'N01', '2020-2024', 'Full-time', 2020),
('L02', 'DS01', 'N02', '2020-2024', 'Full-time', 2020),
('L03', 'AM01', 'N03', '2020-2024', 'Full-time', 2020);

-- Insert sample data into 'sinhvien' table
INSERT INTO sinhvien (MaSV, HoTen, MaLop, GioiTinh, NgaySinh, DiaChi) VALUES
('SV01', 'John Doe', 'L01', 'Male', '2000-01-01', '123 Main St'),
('SV02', 'Jane Smith', 'L02', 'Female', '2000-02-01', '456 Oak St'),
('SV03', 'Alice Johnson', 'L03', 'Female', '2000-03-01', '789 Pine St');

-- Insert sample data into 'hocphan' table
INSERT INTO hocphan (MaHP, TenHP, SoDVHT, MaNganh, HocKy) VALUES
('HP01', 'Introduction to Programming', 3, 'N01', 1),
('HP02', 'Data Structures', 3, 'N01', 2),
('HP03', 'Calculus I', 4, 'N02', 1);

-- Insert sample data into 'diemhp' table
INSERT INTO diemhp (MaSV, MaHP, DiemHP) VALUES
('SV01', 'HP01', 85),
('SV01', 'HP02', 90),
('SV02', 'HP03', 95),
('SV03', 'HP01', 75),
('SV03', 'HP03', 80);
55 changes: 55 additions & 0 deletions db/ini_table.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- Create the 'khoa' table
CREATE TABLE khoa (
MaKhoa CHAR(10) PRIMARY KEY,
TenKhoa VARCHAR(100) NOT NULL
);

-- Create the 'nganh' table
CREATE TABLE nganh (
MaNganh CHAR(10) PRIMARY KEY,
TenNganh VARCHAR(100) NOT NULL,
MaKhoa CHAR(10) NOT NULL,
FOREIGN KEY (MaKhoa) REFERENCES khoa(MaKhoa)
);

-- Create the 'lop' table
CREATE TABLE lop (
MaLop CHAR(10) PRIMARY KEY,
TenLop VARCHAR(100) NOT NULL,
MaNganh CHAR(10) NOT NULL,
KhoaHoc VARCHAR(20) NOT NULL,
HeDT VARCHAR(20) NOT NULL,
NamNhapHoc YEAR NOT NULL,
FOREIGN KEY (MaNganh) REFERENCES nganh(MaNganh)
);

-- Create the 'sinhvien' table
CREATE TABLE sinhvien (
MaSV CHAR(10) PRIMARY KEY,
HoTen VARCHAR(100) NOT NULL,
MaLop CHAR(10) NOT NULL,
GioiTinh VARCHAR(10) NOT NULL,
NgaySinh DATE NOT NULL,
DiaChi VARCHAR(255) NOT NULL,
FOREIGN KEY (MaLop) REFERENCES lop(MaLop)
);

-- Create the 'hocphan' table
CREATE TABLE hocphan (
MaHP CHAR(10) PRIMARY KEY,
TenHP VARCHAR(100) NOT NULL,
SoDVHT INT NOT NULL,
MaNganh CHAR(10) NOT NULL,
HocKy INT NOT NULL,
FOREIGN KEY (MaNganh) REFERENCES nganh(MaNganh)
);

-- Create the 'diemhp' table
CREATE TABLE diemhp (
MaSV CHAR(10) NOT NULL,
MaHP CHAR(10) NOT NULL,
DiemHP FLOAT NOT NULL,
PRIMARY KEY (MaSV, MaHP),
FOREIGN KEY (MaSV) REFERENCES sinhvien(MaSV),
FOREIGN KEY (MaHP) REFERENCES hocphan(MaHP)
);
22 changes: 22 additions & 0 deletions delete_student.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header("Location: login.php");
exit();
}

include_once 'functions/student_functions.php';

if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['id'])) {
$student_id = $_GET['id'];

// Perform the deletion
if (deleteStudent($student_id)) {
echo "Student deleted successfully.";
header("Location: all_students.php");
} else {
echo "Error deleting student.";
}
} else {
echo "Invalid request.";
}
8 changes: 8 additions & 0 deletions functions/class_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

function getAllClasses()
{
global $conn;
$sql = "SELECT MaLop, TenLop FROM lop";
return $conn->query($sql);
}
39 changes: 39 additions & 0 deletions functions/search_students.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
// Include necessary files and initialize database connection
session_start();
include 'student_functions.php';

// Check if the search term is provided in the request
if (isset($_GET['search'])) {
// Get the search term from the request
$searchTerm = $_GET['search'];

// Call the function to search for students based on the provided search term
$filteredStudents = searchStudents($searchTerm);

// Check if any students are found
if ($filteredStudents->num_rows > 0) {
// Display the filtered student data
echo "<table><tr><th>ID</th><th>Name</th><th>Class</th><th>DOB</th><th>Address</th><th>Actions</th></tr>";
while ($row = $filteredStudents->fetch_assoc()) {
echo "<tr>
<td>{$row['MaSV']}</td>
<td>{$row['HoTen']}</td>
<td>{$row['MaLop']}</td>
<td>{$row['NgaySinh']}</td>
<td>{$row['DiaChi']}</td>
<td>
<button onclick=\"openPopup('update_student.php?id={$row['MaSV']}')\">Update</button>
<button onclick=\"openPopup('delete_student.php?id={$row['MaSV']}')\">Delete</button>
</td>
</tr>";
}
echo "</table>";
} else {
// No students found with the given search term
echo "No students found.";
}
} else {
// No search term provided in the request
echo "No search term provided.";
}
Loading

0 comments on commit af13e9d

Please sign in to comment.