CORES
CORES
to fail.
Let's break them down and explain the problems with clarity and solutions.
The delete.php script is expecting an Id parameter, but your SQL script uses the variable $CID,
which is being retrieved using $_REQUEST['ID'].
Ensure that the parameter passed in the delete link matches the one expected by your PHP script.
Update the delete link to use ID as the parameter name.
php
Copy code
<td><a href="delete.php?ID=<?php echo $row['CID']; ?>">Delete</a></td>
php
Copy code
$CID = mysqli_real_escape_string($con, $_REQUEST['ID']);
However:
If the ID parameter is missing in the URL, $CID will be empty, and the SQL query will
not work.
This can lead to unexpected behavior or errors.
Solution: Check if ID is Set
Updated Code:
php
Copy code
if (isset($_REQUEST['ID'])) {
$CID = (int)$_REQUEST['ID']; // Cast to integer for safety
} else {
die("Error: No course ID provided.");
}
php
Copy code
header("Location: Delete_Student.php");
This is incorrect because you are working with courses, not students.
Corrected Code:
php
Copy code
header("Location: course.html");
The current script does not give detailed feedback if something fails (e.g., database errors).
if (!mysqli_stmt_execute($stmt)) {
die("Error executing query: " . mysqli_error($con));
}
php
Copy code
<?php
// Database connection
$con = mysqli_connect("localhost", "root", "", "school");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_stmt_execute($stmt)) {
// Successfully deleted, redirect to the Course page
header("Location: course.html");
exit();
} else {
die("Error executing query: " . mysqli_error($con));
}
php
Copy code
<table border="2px" cellpadding="10px" cellspacing="4px">
<tr><th>CID</th><th>CName</th><th>Department</th><th>Action</th></tr>
<?php
$con = mysqli_connect("localhost", "root", "", "school");
if ($con) {
$sql = mysqli_query($con, "SELECT * FROM course");
while ($row = mysqli_fetch_array($sql)) {
echo '<tr><td>'.$row['CID'].'</td>';
echo '<td>'.$row['CName'].'</td>';
echo '<td>'.$row['Department'].'</td>';
?>
<td><a href="delete.php?ID=<?php echo $row['CID'];
?>">Delete</a></td></tr>
<?php
}
} else {
echo "Connection failed: " . mysqli_connect_error();
}
?>
</table>
Debugging Steps
Test if the delete link correctly passes the ID to delete.php.
Ensure the delete.php script deletes the record from the database.
Verify that users are redirected to course.html after successful deletion.
Check for any error messages for debugging.