-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_character.php
More file actions
56 lines (46 loc) · 1.46 KB
/
delete_character.php
File metadata and controls
56 lines (46 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!----------------------------------------------------------
DELETE PAGE – delete_character.php – The "D" in CRUD
------------------------------------------------------------
This page mirrors the Skellig film-delete.php workflow:
1. Connect to DB ✅
2. Get the character ID (PK) via GET ✅
3. Validate it ✅
4. DELETE the matching row ✅
5. Redirect back to the READ page ✅
TABLES:
- t_characters
PRIMARY KEY: character_id
----------------------------------------------------------->
<?php
require "includes/db.php";
// includes/db.php opens the MySQLi connection and assigns it to $conn.
/*
STEP 2 ✅ Retrieve the PRIMARY KEY (character_id) from the URL.
Example: delete_character.php?id=5
*/
$id = (int) ($_GET["id"] ?? 0);
/*
STEP 3 ✅ Basic validation.
If id is missing, zero or nonsense, stop early to avoid bad SQL.
*/
if ($id <= 0) {
die("Invalid character ID.");
}
/*
STEP 4 ✅ DELETE the record from t_characters.
WHERE character_id = ? ensures ONLY the row with this PK is deleted.
*/
$stmt = mysqli_prepare($conn, "DELETE FROM t_characters WHERE character_id = ?");
if ($stmt === false) {
die("Prepare failed: " . mysqli_error($conn));
}
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($conn);
/*
STEP 5 ✅ Redirect back to the READ page.
*/
header("Location: list_characters.php");
exit();
?>