Your code involves two parts:
1. HTML + PHP script to display the list of teachers and provide a delete link.
2. PHP script to handle the deletion of a teacher record from the database.
Let's analyze the issues and solutions step by step.
1. HTML Table with Delete Link
The issue is with the delete link:
php
Copy code
<td><a href="delete.php?Id=<?php echo $row['TID']; ?>">Delete</a></td>
Problem 1: URL Parameter Mismatch
The link sends the parameter Id to delete.php.
But the delete.php script looks for $_REQUEST['ID']. Notice that Id and ID have
different casing.
PHP is case-sensitive for array keys, so $_REQUEST['ID'] will not find the value passed
as Id.
Solution: Consistent Parameter Name
Use the same parameter name in both the HTML table and the PHP script.
Fix in the HTML Link:
Change Id to ID to match the PHP script:
php
Copy code
<td><a href="delete.php?ID=<?php echo $row['TID']; ?>">Delete</a></td>
2. PHP Script for Deletion
The PHP script deletes a teacher based on the ID passed through the URL. There are several
issues here:
Problem 2: No Check for ID Parameter
If ID is not provided in the URL, the script will attempt to delete without a valid TID. This can
result in errors or unintended behavior.
Solution: Verify ID Parameter Exists
Add a check to ensure ID is passed before proceeding:
php
Copy code
if (isset($_REQUEST['ID'])) {
$TID = (int)$_REQUEST['ID']; // Cast to integer for safety
} else {
die("Error: No teacher ID provided.");
}
Problem 3: SQL Injection Vulnerability
While you use mysqli_real_escape_string() to sanitize input, it's better to use prepared
statements to prevent SQL injection.
Solution: Use Prepared Statements
You're already attempting to use prepared statements (mysqli_prepare). Ensure they are
implemented properly:
php
Copy code
$sql = "DELETE FROM `teacher_table` WHERE TID = ?";
Bind the parameter securely:
php
Copy code
mysqli_stmt_bind_param($stmt, "i", $TID);
Problem 4: Misleading Redirect
After successful deletion, the script redirects to Delete_Student.php. This is incorrect because:
The script handles teacher records, not student records.
Solution: Redirect to an Appropriate Page
Redirect to a teacher-related page, such as teacher.html:
php
Copy code
header("Location: teacher.html");
exit();
Problem 5: Lack of Error Feedback
Your script does not provide detailed feedback if an error occurs while connecting to the
database or executing the query. This makes debugging difficult.
Solution: Add Error Messages
Display meaningful error messages:
php
Copy code
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
if (!mysqli_stmt_execute($stmt)) {
die("Error executing query: " . mysqli_error($con));
}
Final Corrected Code
HTML Table with Delete Link
php
Copy code
<table border="2px" cellpadding="10px" cellspacing="4px">
<tr><th>teacher ID</th><th>teacher Name</th><th>Major</th><th>Action</th></tr>
<?php
$con = mysqli_connect("localhost", "root", "", "school");
if ($con) {
$sql = mysqli_query($con, "SELECT * FROM teacher_table");
while ($row = mysqli_fetch_array($sql)) {
echo '<tr><td>'.$row['TID'].'</td>';
echo '<td>'.$row['TName'].'</td>';
echo '<td>'.$row['Major'].'</td>';
?>
<td><a href="delete.php?ID=<?php echo $row['TID'];
?>">Delete</a></td></tr>
<?php
}
} else {
echo "Connection failed: " . mysqli_connect_error();
}
?>
</table>
PHP Script to Delete Teacher
php
Copy code
<?php
// Connect to the database
$con = mysqli_connect("localhost", "root", "", "school");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
// Check if 'ID' parameter is passed
if (isset($_REQUEST['ID'])) {
$TID = (int)$_REQUEST['ID']; // Cast to integer for safety
} else {
die("Error: No teacher ID provided.");
}
// Prepare the DELETE query
$sql = "DELETE FROM `teacher_table` WHERE TID = ?";
if ($stmt = mysqli_prepare($con, $sql)) {
mysqli_stmt_bind_param($stmt, "i", $TID); // Bind the ID as an integer
if (mysqli_stmt_execute($stmt)) {
// Successfully deleted, redirect to the Teacher page
header("Location: teacher.html");
exit();
} else {
die("Error executing query: " . mysqli_error($con));
}
mysqli_stmt_close($stmt); // Close the prepared statement
} else {
die("Error preparing query: " . mysqli_error($con));
}
mysqli_close($con); // Close the database connection
?>
How It Works
1. Consistent Parameter Name: The ID parameter is consistent in both the HTML and
PHP scripts.
2. Validation: The script checks if ID is passed before proceeding.
3. Prepared Statements: Secure against SQL injection.
4. Error Handling: Provides clear error messages for debugging.
5. Proper Redirection: Redirects to the appropriate teacher page after deletion.
Debugging Tips
Test the delete.php script by visiting delete.php?ID=1 (replace 1 with a valid teacher
ID in your database).
Verify that the record is deleted and the user is redirected to teacher.html.
If you encounter issues, check your database connection and ensure the teacher_table
and column names (TID, etc.) are correct.