How can PHP handle the deletion of multiple entries selected through checkboxes without errors?
When handling the deletion of multiple entries selected through checkboxes in PHP, you can use a loop to iterate through the selected checkboxes and delete the corresponding entries one by one. Make sure to properly sanitize and validate the input data to prevent any security vulnerabilities. Additionally, consider using prepared statements to prevent SQL injection attacks.
// Assuming form is submitted with checkboxes named 'delete[]' containing IDs of entries to be deleted
if(isset($_POST['delete'])) {
$ids = $_POST['delete'];
// Sanitize and validate the IDs
foreach($ids as $id) {
$id = filter_var($id, FILTER_SANITIZE_NUMBER_INT);
// Perform deletion query using prepared statements
$stmt = $pdo->prepare("DELETE FROM table_name WHERE id = ?");
$stmt->execute([$id]);
}
echo "Selected entries have been deleted successfully.";
}
Keywords
Related Questions
- What are some common pitfalls when storing passwords in PHP?
- What are common pitfalls when handling form data in PHP, and how can they be avoided?
- What strategies can be implemented to troubleshoot PHP code within the context of browser games to address rendering issues without providing direct access to the game itself?