0% found this document useful (0 votes)
15 views5 pages

CORES

Uploaded by

Bu MULUtam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

CORES

Uploaded by

Bu MULUtam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

The two forms you provided have the following issues that are likely causing the code

to fail.
Let's break them down and explain the problems with clarity and solutions.

1. HTML Table with Delete Link (Course Table)


Problem 1: Incorrect URL Parameter in Delete Link

The delete.php script is expecting an Id parameter, but your SQL script uses the variable $CID,
which is being retrieved using $_REQUEST['ID'].

This discrepancy will cause an issue because:

 The delete link in the table uses delete.php?Id=....


 Your PHP script is looking for $_REQUEST['ID'], which doesn’t match the Id parameter
in the URL.

Solution: Fix the URL Parameter Name

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.

Corrected Code for Delete Link:

php
Copy code
<td><a href="delete.php?ID=<?php echo $row['CID']; ?>">Delete</a></td>

2. PHP Script for Deleting a Course


Problem 2: Improper Retrieval of the ID

The PHP script is trying to retrieve the CID using:

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

Add a check to ensure that ID is passed in the URL before proceeding.

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.");
}

Problem 3: Redirection URL is Misleading

Your PHP script redirects to Delete_Student.php after deleting a course:

php
Copy code
header("Location: Delete_Student.php");

This is incorrect because you are working with courses, not students.

Solution: Redirect to a Course-Specific Page

Update the redirection to a more appropriate page, such as course.html.

Corrected Code:

php
Copy code
header("Location: course.html");

3. General PHP Debugging and Best Practices


Here are additional improvements to make the PHP script more reliable:

Problem 4: Lack of Error Messages for SQL Issues

The current script does not give detailed feedback if something fails (e.g., database errors).

Solution: Use mysqli_error() for Debugging

Add error messages to display detailed issues:


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 PHP Script


Here’s the fully corrected and debugged PHP script:

php
Copy code
<?php
// Database connection
$con = mysqli_connect("localhost", "root", "", "school");

if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}

// Check if 'ID' is passed in the URL


if (isset($_REQUEST['ID'])) {
$CID = (int)$_REQUEST['ID']; // Cast to integer for safety
} else {
die("Error: No course ID provided.");
}

// Prepare the SQL query to delete the course


$sql = "DELETE FROM course WHERE CID = ?";
if ($stmt = mysqli_prepare($con, $sql)) {
mysqli_stmt_bind_param($stmt, "i", $CID); // Bind the parameter as an
integer

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));
}

// Close the statement


mysqli_stmt_close($stmt);
} else {
die("Error preparing query: " . mysqli_error($con));
}

// Close the database connection


mysqli_close($con);
?>
Final Corrected HTML Table
Here’s the corrected HTML for displaying the course table with proper delete links:

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>

How the Fix Works


1. Proper Parameter Name: Ensures the delete.php script retrieves the correct ID
parameter.
2. Error Handling: Adds checks for missing parameters and SQL errors.
3. Redirection: Redirects users to the correct page after deletion.
4. Prepared Statements: Protects the database from SQL injection.

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.

Let me know if you encounter further issues!


4o

You might also like