PHP删除功能无法将ID传递至URL的问题排查
Let's walk through the issues step by step to get that delete button working properly:
Verify the
$idvariable actually holds a value
Before you build the delete link, add a quick check to make sure$idisn't empty. If this variable is null or blank from the start, the URL will never get the ID parameter. Add this line right before generating the link:var_dump($id); // Shows you exactly what's stored in $id exit; // Stops execution so you can see the resultIf this outputs
NULLor an empty string, you need to fix how you're retrieving the ID for the current entry (double-check where$idis set earlier in your code).Fix the
hrefattribute quoting
Your current link code doesn't wrap thehrefvalue in quotes, which can cause browsers to misparse the URL and drop theidparameter entirely. Update the link generation code to properly quote the href:// Option 1: Escape double quotes inside the string $content .= "<td><a href=\"/index.php?p=3&id=$id\">delete</a></td>"; // Option 2: Use single quotes for the outer string to avoid escaping $content .= '<td><a href="/index.php?p=3&id=' . $id . '">delete</a></td>';Either version ensures the browser reads the full URL including the
idparameter.Fix the function name mismatch
There's a typo in your function calls that will break the delete logic even if the ID is passed correctly:- Your case logic calls
$items->deleteItem($_GET['id'])(singular "Item") - But your class defines
public function deleteItems($id)(plural "Items")
Align these names by either updating the call to use the plural version:
case 3: $items = new dbconnection(); $content = $items->deleteItems($_GET['id']); break;Or rename the class function to
deleteItemto match the call.- Your case logic calls
Confirm the GET parameter is being received
Once you've fixed the above, add a check in your case 3 logic to verify theidparameter is coming through:case 3: var_dump($_GET); // Shows all received GET parameters exit; // Rest of your code...If you see
['p' => '3', 'id' => 'your-desired-id'], then the parameter is being passed correctly, and you can proceed to debug the delete query if needed.
After addressing these points, your delete link should properly pass the ID parameter, and the delete logic should execute as expected.
内容的提问来源于stack exchange,提问作者Djem

