Overview
This project demonstrates how to create a simple student information management system using PHP and MySQL. The system includes two main modules: basic CRUD operations for student records and a user authentication system with profile management.
Database Setup
First, create a MySQL database named student_db with two tables:
Table: students
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
gender ENUM('Male', 'Female') NOT NULL,
interests TEXT,
residence VARCHAR(100),
notes TEXT
);
Table: user_profiles
CREATE TABLE user_profiles (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
gender ENUM('Male', 'Female') NOT NULL,
age INT,
residence VARCHAR(100),
skills TEXT
);
Module 1: Student CRUD Operations
Database Configuration
Create a configuration file for database connection:
<?php // db_config.php
$dbHost = "127.0.0.1";
$dbUser = "root";
$dbPass = "101032";
$dbName = "student_db";
$dbLink = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($dbLink-?>connect_error) {
die("Database connection failed: " . $dbLink->connect_error);
}
$dbLink->set_charset("utf8");
?>
Add Student Form
Create a form to input student information:
<?php // add_student_form.php
?>
<meta charset="UTF-8"></meta>
<title>Add Student Record</title>
<style>
body { font-family: Arial, sans-serif; }
.container { max-width: 600px; margin: 50px auto; }
.form-group { margin-bottom: 15px; }
label { display: inline-block; width: 120px; }
input[type="text"], select, textarea { width: 300px; padding: 5px; }
table { border-collapse: collapse; margin: 0 auto; }
td { padding: 10px; border: 1px solid #ddd; }
</style>
<div class="container">
<h2>Add Student Information</h2>
<form action="process_add_student.php" method="post">
<table>
<tr>
<td><label for="fullName">Full Name:</label></td>
<td><input id="fullName" name="fullName" required="" type="text"></input></td>
</tr>
<tr>
<td><label>Gender:</label></td>
<td>
<input id="male" name="gender" type="radio" value="Male"></input> <label for="male">Male</label>
<input id="female" name="gender" type="radio" value="Female"></input> <label for="female">Female</label>
</td>
</tr>
<tr>
<td><label for="interests">Interests:</label></td>
<td><input id="interests" name="interests" type="text"></input></td>
</tr>
<tr>
<td><label for="residence">Residence:</label></td>
<td>
<select id="residence" name="residence">
<option value="">Select Residence</option>
<option value="Shijiazhuang">Shijiazhuang</option>
<option value="Baoding">Baoding</option>
<option value="Qinhuangdao">Qinhuangdao</option>
</select>
</td>
</tr>
<tr>
<td><label for="notes">Notes:</label></td>
<td><textarea id="notes" name="notes" rows="4"></textarea></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</td>
</tr>
</table>
</form>
</div>
Process Add Student
Handle the form submission and insert data into the database:
<?php // process_add_student.php
header("Content-Type: text/html; charset=utf-8");
require_once 'db_config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fullName = trim($_POST['fullName']);
$gender = $_POST['gender'];
$interests = trim($_POST['interests']);
$residence = $_POST['residence'];
$notes = trim($_POST['notes']);
$query = "INSERT INTO students (full_name, gender, interests, residence, notes) VALUES (?, ?, ?, ?, ?)";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("sssss", $fullName, $gender, $interests, $residence, $notes);
if ($stmt->execute()) {
echo "<script>alert('Student added successfully'); window.location.href='student_list.php';</script>";
} else {
echo "<script>alert('Failed to add student'); history.back();</script>";
}
$stmt->close();
}
$dbLink->close();
?>
Student List Display
Show all student records with edit and delete options:
<?php // student_list.php
?>
<meta charset="UTF-8"></meta>
<title>Student Records</title>
<style>
table { border-collapse: collapse; width: 90%; margin: 20px auto; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
th { background-color: #f2f2f2; }
.action-links a { margin: 0 5px; }
</style>
<h2 style="text-align: center;">Student Information Table</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Gender</th>
<th>Interests</th>
<th>Residence</th>
<th>Notes</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php require_once 'db_config.php';
$query = "SELECT * FROM students ORDER BY id DESC";
$result = $dbLink-?>query($query);
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['full_name']}</td>";
echo "<td>{$row['gender']}</td>";
echo "<td>{$row['interests']}</td>";
echo "<td>{$row['residence']}</td>";
echo "<td>{$row['notes']}</td>";
echo "<td class="action-links">";
echo "<a href="edit_student_form.php?id={$row[" id="">Edit</a> | ";
echo "<a confirm="" href="delete_student.php?id={$row[" id="" onclick="\"return" sure="" you="">Delete</a>";
echo "</td>";
echo "</tr>";
}
$dbLink->close();
?>
</tbody>
</table>
Delete Student
Handle student deletion:
<?php // delete_student.php
header("Content-Type: text/html; charset=utf-8");
require_once 'db_config.php';
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$studentId = $_GET['id'];
$query = "DELETE FROM students WHERE id = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("i", $studentId);
if ($stmt->execute()) {
echo "<script>alert('Student deleted successfully'); window.location.href='student_list.php';</script>";
} else {
echo "<script>alert('Failed to delete student'); history.back();</script>";
}
$stmt->close();
}
$dbLink->close();
?>
Edit Student Form
Display form with existing student data:
<?php // edit_student_form.php
?>
<meta charset="UTF-8"></meta>
<title>Edit Student Record</title>
<style>
body { font-family: Arial, sans-serif; }
.container { max-width: 600px; margin: 50px auto; }
table { border-collapse: collapse; margin: 0 auto; }
td { padding: 10px; border: 1px solid #ddd; }
</style>
<div class="container">
<h2>Edit Student Information</h2>
<?php require_once 'db_config.php';
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$studentId = $_GET['id'];
$query = "SELECT * FROM students WHERE id = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("i", $studentId);
$stmt->execute();
$result = $stmt->get_result();
$student = $result->fetch_assoc();
?>
<form action="process_edit_student.php" method="post">
<table>
<tr>
<td>ID:</td>
<td><input name="studentId" readonly="readonly" type="text" value="<?php echo $student['id']; ?>"></input></td>
</tr>
<tr>
<td>Full Name:</td>
<td><input name="fullName" required="" type="text" value="<?php echo htmlspecialchars($student['full_name']); ?>"></input></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input echo="" if="" name="gender" type="radio" value="Male"></input>> Male
<input echo="" if="" name="gender" type="radio" value="Female"></input>> Female
</td>
</tr>
<tr>
<td>Interests:</td>
<td><input name="interests" type="text" value="<?php echo htmlspecialchars($student['interests']); ?>"></input></td>
</tr>
<tr>
<td>Residence:</td>
<td>
<select name="residence">
<option value="">Select Residence</option>
<option echo="" if="" value="Shijiazhuang">>Shijiazhuang</option>
<option echo="" if="" value="Baoding">>Baoding</option>
<option echo="" if="" value="Qinhuangdao">>Qinhuangdao</option>
</select>
</td>
</tr>
<tr>
<td>Notes:</td>
<td><textarea name="notes" rows="4"><?php echo htmlspecialchars($student['notes']); ??></textarea></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<button type="submit">Update</button>
<button type="reset">Reset</button>
</td>
</tr>
</table>
</form>
<?php }
$dbLink-?>close();
?>
</div>
Process Edit Student
Update student information in the database:
<?php // process_edit_student.php
header("Content-Type: text/html; charset=utf-8");
require_once 'db_config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$studentId = $_POST['studentId'];
$fullName = trim($_POST['fullName']);
$gender = $_POST['gender'];
$interests = trim($_POST['interests']);
$residence = $_POST['residence'];
$notes = trim($_POST['notes']);
$query = "UPDATE students SET full_name = ?, gender = ?, interests = ?, residence = ?, notes = ? WHERE id = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("sssssi", $fullName, $gender, $interests, $residence, $notes, $studentId);
if ($stmt->execute()) {
echo "<script>alert('Student updated successfully'); window.location.href='student_list.php';</script>";
} else {
echo "<script>alert('Failed to update student'); history.back();</script>";
}
$stmt->close();
}
$dbLink->close();
?>
Module 2: User Authentication & Profile Management
Login Page
Create a login form that validates user credentials:
<?php // login.php
?>
<meta charset="UTF-8"></meta>
<title>User Login</title>
<style>
.login-container { max-width: 400px; margin: 100px auto; padding: 20px; border: 1px solid #ccc; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; }
input[type="text"], input[type="radio"] { margin-right: 5px; }
</style>
<div class="login-container">
<h2>User Login</h2>
<form action="" method="post">
<div class="form-group">
<label for="userName">Full Name:</label>
<input id="userName" name="userName" required="" type="text"></input>
</div>
<div class="form-group">
<label>Gender:</label>
<input id="maleLogin" name="userGender" type="radio" value="Male"></input> <label for="maleLogin">Male</label>
<input id="femaleLogin" name="userGender" type="radio" value="Female"></input> <label for="femaleLogin">Female</label>
</div>
<div class="form-group">
<button type="submit">Login</button>
</div>
</form>
</div>
<?php require_once 'db_config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$userName = trim($_POST['userName']);
$userGender = $_POST['userGender'];
if (!empty($userName) && !empty($userGender)) {
$query = "SELECT id FROM user_profiles WHERE full_name = ? AND gender = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("ss", $userName, $userGender);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 1) {
$stmt->bind_result($userId);
$stmt->fetch();
echo "<script>alert('Login successful'); window.location.href='profile.php?id={$userId}';</script>";
} else {
echo "<script>alert('Login failed'); window.location.href='register.php?name={$userName}&gender={$userGender}';</script>";
}
$stmt->close();
} else {
echo "<script>alert('Please complete all fields'); history.back();</script>";
}
}
$dbLink->close();
?>
Registration Page
Display registration form when login fails:
<?php // register.php
?>
<meta charset="UTF-8"></meta>
<title>Complete Your Profile</title>
<style>
body { font-family: Arial, sans-serif; }
.message { text-align: center; color: green; margin: 20px; }
.container { max-width: 600px; margin: 0 auto; }
table { border-collapse: collapse; width: 100%; }
td { padding: 10px; border: 1px solid #ddd; }
</style>
<?php require_once 'db_config.php';
if (isset($_GET['name']) && isset($_GET['gender'])) {
$name = htmlspecialchars($_GET['name']);
$gender = $_GET['gender'];
$title = ($gender === 'Male') ? 'Mr.' : 'Ms.';
echo "<h3 class='message'?>Dear {$title} {$name}, we couldn't find your profile. Please complete your details below.";
}
?>
<hr style="border: 1px solid red;"></hr>
<div class="container">
<h2>Add Personal Information</h2>
<form action="save_user.php" method="post">
<table>
<tr>
<td>Full Name:</td>
<td><input name="fullName" required="" type="text"></input></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input name="gender" type="radio" value="Male"></input> Male
<input name="gender" type="radio" value="Female"></input> Female
</td>
</tr>
<tr>
<td>Age:</td>
<td>
<input name="age" type="radio" value="17"></input> 17
<input name="age" type="radio" value="18"></input> 18
<input name="age" type="radio" value="19"></input> 19
<input name="age" type="radio" value="20"></input> 20
</td>
</tr>
<tr>
<td>Residence:</td>
<td>
<select name="residence">
<option value="">Select Residence</option>
<option value="Shijiazhuang">Shijiazhuang</option>
<option value="Baoding">Baoding</option>
<option value="Qinhuangdao">Qinhuangdao</option>
</select>
</td>
</tr>
<tr>
<td>Skills & Hobbies:</td>
<td><textarea name="skills" rows="4"></textarea></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<button type="submit">Save</button>
<button type="reset">Reset</button>
</td>
</tr>
</table>
</form>
</div>
Save User Profile
Process new user registration:
<?php // save_user.php
header("Content-Type: text/html; charset=utf-8");
require_once 'db_config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fullName = trim($_POST['fullName']);
$gender = $_POST['gender'];
$age = intval($_POST['age']);
$residence = $_POST['residence'];
$skills = trim($_POST['skills']);
$query = "INSERT INTO user_profiles (full_name, gender, age, residence, skills) VALUES (?, ?, ?, ?, ?)";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("ssiss", $fullName, $gender, $age, $residence, $skills);
if ($stmt->execute()) {
echo "<script>alert('Profile saved successfully'); window.location.href='login.php';</script>";
} else {
echo "<script>alert('Failed to save profile'); history.back();</script>";
}
$stmt->close();
}
$dbLink->close();
?>
User Profile Display
Show user profile with edit and delete options:
<?php // profile.php
?>
<meta charset="UTF-8"></meta>
<title>User Profile</title>
<style>
body { font-family: Arial, sans-serif; }
.profile-header { text-align: center; color: green; }
.container { max-width: 600px; margin: 20px auto; }
table { border-collapse: collapse; width: 100%; }
td { padding: 10px; border: 1px solid #ddd; }
.action-links { text-align: center; }
</style>
<?php require_once 'db_config.php';
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$userId = $_GET['id'];
$query = "SELECT * FROM user_profiles WHERE id = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
$profile = $result->fetch_assoc();
echo "<h3 class="profile-header">Your Profile Information:</h3>";
echo "<hr style="border: 1px solid red;"></hr>";
?>
<div class="container">
<table>
<tr>
<td>Full Name:</td>
<td><?php echo htmlspecialchars($profile['full_name']); ??></td>
</tr>
<tr>
<td>Gender:</td>
<td><?php echo $profile['gender']; ??></td>
</tr>
<tr>
<td>Age:</td>
<td><?php echo $profile['age']; ??></td>
</tr>
<tr>
<td>Residence:</td>
<td><?php echo htmlspecialchars($profile['residence']); ??></td>
</tr>
<tr>
<td>Skills & Hobbies:</td>
<td><?php echo htmlspecialchars($profile['skills']); ??></td>
</tr>
<tr>
<td class="action-links" colspan="2">
<a href="edit_profile_form.php?id=<?php echo $profile['id']; ?>">Edit</a> |
<a href="delete_profile.php?id=<?php echo $profile['id']; ?>" onclick="return confirm('Delete this profile?')">Delete</a>
</td>
</tr>
<tr>
<td class="action-links" colspan="2">
<a href="login.php">Back to Login</a>
</td>
</tr>
</table>
</div>
<?php }
$dbLink-?>close();
?>
Delete Profile
Handle profile deletion:
<?php // delete_profile.php
header("Content-Type: text/html; charset=utf-8");
require_once 'db_config.php';
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$userId = $_GET['id'];
$query = "DELETE FROM user_profiles WHERE id = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("i", $userId);
if ($stmt->execute()) {
echo "<script>alert('Profile deleted successfully'); window.location.href='login.php';</script>";
} else {
echo "<script>alert('Failed to delete profile'); history.back();</script>";
}
$stmt->close();
}
$dbLink->close();
?>
Edit Profile Form
Display form to edit user profile:
<?php // edit_profile_form.php
?>
<meta charset="UTF-8"></meta>
<title>Edit Profile</title>
<style>
body { font-family: Arial, sans-serif; }
.container { max-width: 600px; margin: 50px auto; }
table { border-collapse: collapse; width: 100%; }
td { padding: 10px; border: 1px solid #ddd; }
</style>
<div class="container">
<h2>Edit Profile</h2>
<?php require_once 'db_config.php';
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$userId = $_GET['id'];
$query = "SELECT * FROM user_profiles WHERE id = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
$profile = $result->fetch_assoc();
?>
<form action="process_edit_profile.php" method="post">
<table>
<tr>
<td>ID:</td>
<td><input name="userId" readonly="readonly" type="text" value="<?php echo $profile['id']; ?>"></input></td>
</tr>
<tr>
<td>Full Name:</td>
<td><input name="fullName" required="" type="text" value="<?php echo htmlspecialchars($profile['full_name']); ?>"></input></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input echo="" if="" name="gender" type="radio" value="Male"></input>> Male
<input echo="" if="" name="gender" type="radio" value="Female"></input>> Female
</td>
</tr>
<tr>
<td>Age:</td>
<td>
<?php for ($i = 17; $i <= 20; $i++) {
$checked = ($profile['age'] == $i) ? 'checked' : '';
echo "<input type='radio' name='age' value='{$i}' {$checked}?> {$i} ";
}
?>
</td>
</tr>
<tr>
<td>Residence:</td>
<td>
<select name="residence">
<option value="">Select Residence</option>
<?php $cities = ['Shijiazhuang', 'Baoding', 'Qinhuangdao'];
foreach ($cities as $city) {
$selected = ($profile['residence'] == $city) ? 'selected' : '';
echo "<option value='{$city}' {$selected}?>{$city}";
}
?>
</select>
</td>
</tr>
<tr>
<td>Skills & Hobbies:</td>
<td><textarea name="skills" rows="4"><?php echo htmlspecialchars($profile['skills']); ??></textarea></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<button type="submit">Update</button>
</td>
</tr>
</table>
</form>
<?php }
$dbLink-?>close();
?>
</div>
Process Edit Profile
Update user profile in the database:
<?php // process_edit_profile.php
header("Content-Type: text/html; charset=utf-8");
require_once 'db_config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$userId = $_POST['userId'];
$fullName = trim($_POST['fullName']);
$gender = $_POST['gender'];
$age = intval($_POST['age']);
$residence = $_POST['residence'];
$skills = trim($_POST['skills']);
$query = "UPDATE user_profiles SET full_name = ?, gender = ?, age = ?, residence = ?, skills = ? WHERE id = ?";
$stmt = $dbLink-?>prepare($query);
$stmt->bind_param("ssissi", $fullName, $gender, $age, $residence, $skills, $userId);
if ($stmt->execute()) {
echo "<script>alert('Profile updated successfully'); window.location.href='profile.php?id={$userId}';</script>";
} else {
echo "<script>alert('Failed to update profile'); history.back();</script>";
}
$stmt->close();
}
$dbLink->close();
?>