Students Management System (SMS) ni mfumo unaowawezesha walimu/administrators:

Kuongeza, kuhariri, na kufuta wanafunzi (CRUD).

Kuangalia orodha ya wanafunzi kwa darasa, jinsia, au score.

Kuonyesha statistics za shule.

Mfumo huu utakuwa secure kwa kutumia PDO, na utumie prepared statements kuzuia SQL Injection.

⚙️ 2. Muundo wa Project

Tengeneza folder la project, mfano: students_management/

students_management/
├── config.php
├── index.php ← View students
├── add_student.php ← Add student
├── edit_student.php ← Edit student
├── delete_student.php ← Delete student
└── logout.php

🧩 3. Database Setup
CREATE DATABASE school_db;

USE school_db;

CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
class VARCHAR(50) NOT NULL,
gender ENUM('Male','Female') NOT NULL,
age INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


💡 Maelezo:

ENUM inasaidia kuweka jinsia.

created_at inarekodi wakati mwanafunzi aliongezwa.

⚙️ 4. Database Connection (config.php)
<?php
$dsn = "mysql:host=localhost;dbname=school_db;charset=utf8mb4";
$username = "root";
$password = "";

try {
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("❌ Connection failed: " . $e->getMessage());
}
?>

➕ 5. Add Student (add_student.php)
<?php
include 'config.php';

if($_SERVER['REQUEST_METHOD'] === 'POST'){
$name = trim($_POST['name']);
$class = trim($_POST['class']);
$gender = $_POST['gender'];
$age = $_POST['age'];

$stmt = $pdo->prepare("INSERT INTO students (name, class, gender, age) VALUES (:name, :class, :gender, :age)");
$stmt->execute([
'name'=>$name,
'class'=>$class,
'gender'=>$gender,
'age'=>$age
]);

echo "<p style='color:green;'>✅ Student added successfully!</p>";
}
?>

<h2>➕ Add Student</h2>
<form method="POST">
<input type="text" name="name" placeholder="Name" required><br><br>
<input type="text" name="class" placeholder="Class" required><br><br>
<select name="gender" required>
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><br><br>
<input type="number" name="age" placeholder="Age" required><br><br>
<button type="submit">Add Student</button>
</form>
<a href="index.php">🔙 Back to Students List</a>

📄 6. View Students (index.php)
<?php
include 'config.php';

$stmt = $pdo->query("SELECT * FROM students ORDER BY id DESC");
?>

<h2>📋 Students List</h2>
<a href="add_student.php">➕ Add Student</a><br><br>
<table border="1" cellpadding="8">
<tr>
<th>ID</th>
<th>Name</th>
<th>Class</th>
<th>Gender</th>
<th>Age</th>
<th>Actions</th>
</tr>

<?php
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['class']}</td>
<td>{$row['gender']}</td>
<td>{$row['age']}</td>
<td>
<a href='edit_student.php?id={$row['id']}'>✏️ Edit</a> |
<a href='delete_student.php?id={$row['id']}' onclick='return confirm(\"Are you sure?\")'>🗑️ Delete</a>
</td>
</tr>";
}
?>
</table>

✏️ 7. Edit Student (edit_student.php)
<?php
include 'config.php';
$id = $_GET['id'];

$stmt = $pdo->prepare("SELECT * FROM students WHERE id = :id");
$stmt->execute(['id'=>$id]);
$student = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$student){
die("❌ Student not found!");
}

if($_SERVER['REQUEST_METHOD'] === 'POST'){
$stmt = $pdo->prepare("UPDATE students SET name=:name, class=:class, gender=:gender, age=:age WHERE id=:id");
$stmt->execute([
'name'=>$_POST['name'],
'class'=>$_POST['class'],
'gender'=>$_POST['gender'],
'age'=>$_POST['age'],
'id'=>$id
]);
echo "<p style='color:green;'>✅ Student updated successfully!</p>";
}
?>

<h2>✏️ Edit Student</h2>
<form method="POST">
<input type="text" name="name" value="<?= htmlspecialchars($student['name']) ?>" required><br><br>
<input type="text" name="class" value="<?= htmlspecialchars($student['class']) ?>" required><br><br>
<select name="gender" required>
<option value="Male" <?= $student['gender']=='Male'?'selected':'' ?>>Male</option>
<option value="Female" <?= $student['gender']=='Female'?'selected':'' ?>>Female</option>
</select><br><br>
<input type="number" name="age" value="<?= $student['age'] ?>" required><br><br>
<button type="submit">Update Student</button>
</form>
<a href="index.php">🔙 Back to Students List</a>

❌ 8. Delete Student (delete_student.php)
<?php
include 'config.php';
$id = $_GET['id'];

$stmt = $pdo->prepare("DELETE FROM students WHERE id=:id");
$stmt->execute(['id'=>$id]);

header("Location: index.php");
exit;
?>

🧠 9. Vidokezo vya Security na Maboresho

PDO + Prepared Statements – Salama dhidi ya SQL Injection.

Input Validation – Hakikisha data sahihi inapoingizwa.

Pagination & Search – Kwa data nyingi, ongeza pagination na search box.

Session & Authentication – Zuia user asiye admin kuingia system.

Frontend Design – Tumia Bootstrap au TailwindCSS kuboresha UI.

✅ 10. Hitimisho

Mfumo huu ni msingi wa Students Management System.

Unaweza kuongeza modules za grades, attendance, reports kwa urahisi.

PDO + prepared statements inaboresha security na kudhibiti data salama.

🔗 Tembelea:

👉 https://www.faulink.com/

Kwa mafunzo zaidi ya PHP, PDO, MySQL, na web systems development.