STUDENTS CRUD
The students table contains:
text
student_id
student_first_name
student_last_name
student_course
student_created_at
text
CREATE → INSERT
READ → SELECT
UPDATE → UPDATE
DELETE → DELETE
2.1 CREATE Student
Use Case
The user enters:
text
First Name
Last Name
Course
Get Form Data
php
$firstName = trim($_POST['student_first_name'] ?? '');
$lastName = trim($_POST['student_last_name'] ?? '');
$course = trim($_POST['student_course'] ?? '');
html
<form method="POST">
<input
type="text"
name="student_first_name"
>
<input
type="text"
name="student_last_name"
>
<input
type="text"
name="student_course"
>
<button type="submit">
Save
</button>
</form>
SQL Query
sql
INSERT INTO students (
student_first_name,
student_last_name,
student_course
)
VALUES (?, ?, ?);
PHP PDO
php
$stmt = $pdo->prepare("
INSERT INTO students (
student_first_name,
student_last_name,
student_course
)
VALUES (?, ?, ?)
");
$stmt->execute([
$firstName,
$lastName,
$course
]);
? characters are placeholders.
The values are supplied through:
php
$stmt->execute([
$firstName,
$lastName,
$course
]);
Database Result
For example:
text
First Name: CLIFF AMADEUS
Last Name: EVANGELIO
Course: BSIT
text
student_id: 5
student_first_name: CLIFF AMADEUS
student_last_name: EVANGELIO
student_course: BSIT
student_created_at: current timestamp
student_id is automatically generated because it uses:
sql
AUTO_INCREMENT
2.2 READ Students
Use Case
Display all students in an HTML table. ---SQL Query
sql
SELECT *
FROM students
ORDER BY student_id DESC;
PHP PDO
php
$stmt = $pdo->query("
SELECT *
FROM students
ORDER BY student_id DESC
");
$students = $stmt->fetchAll();
fetchAll() retrieves all matching records.
---
Display the Result
php
<?php foreach ($students as $student): ?>
<?= htmlspecialchars($student['student_id']) ?>
<?= htmlspecialchars($student['student_first_name']) ?>
<?= htmlspecialchars($student['student_last_name']) ?>
<?= htmlspecialchars($student['student_course']) ?>
<?php endforeach; ?>
Read One Student
When editing a student, we first retrieve one record. URL:
text
index.php?section=students&action=update&id=1
php
$studentId = (int) ($_GET['id'] ?? 0);
sql
SELECT *
FROM students
WHERE student_id = ?;
php
$stmt = $pdo->prepare("
SELECT *
FROM students
WHERE student_id = ?
");
$stmt->execute([
$studentId
]);
$student = $stmt->fetch();
fetch() retrieves one record.
---
2.3 UPDATE Student
Use Case
The user edits:
text
First Name
Last Name
Course
SQL Query
sql
UPDATE students
SET
student_first_name = ?,
student_last_name = ?,
student_course = ?
WHERE student_id = ?;
PHP PDO
php
$stmt = $pdo->prepare("
UPDATE students
SET
student_first_name = ?,
student_last_name = ?,
student_course = ?
WHERE student_id = ?
");
$stmt->execute([
$firstName,
$lastName,
$course,
$studentId
]);
WHERE clause identifies which student should be updated.
For example:
sql
UPDATE students
SET
student_first_name = 'CLIFF',
student_last_name = 'EVANGELIO',
student_course = 'BSIT'
WHERE student_id = 1;
1 is updated.
---
2.4 DELETE Student
Use Case
The user clicks:
text
Delete
Get ID
php
$studentId = (int) ($_GET['id'] ?? 0);
SQL Query
sql
DELETE FROM students
WHERE student_id = ?;
PHP PDO
php
$stmt = $pdo->prepare("
DELETE FROM students
WHERE student_id = ?
");
$stmt->execute([
$studentId
]);
Important: Foreign Key Restriction
The database contains:
sql
ON DELETE RESTRICT
borrow and students.
Therefore, if a student has a borrow record, the student cannot be deleted.
For example:
text
Student #1
|
v
Borrow #1
2.5 Students CRUD Summary
text
| CRUD | SQL | PHP Method |
|---|---|---|
| Create | `INSERT` | `prepare()` + `execute()` |
| Read | `SELECT` | `query()` + `fetchAll()` |
| Read One | `SELECT WHERE` | `prepare()` + `fetch()` |
| Update | `UPDATE` | `prepare()` + `execute()` |
| Delete | `DELETE` | `prepare()` + `execute()` |
Sign in to save your progress permanently.
Progress is saved in your browser session
Step 3 of 9