How can PHP be used to pass and retrieve parameters for deleting specific data entries in a database?
To delete specific data entries in a database using PHP, you can pass parameters through the URL or a form submission. These parameters can then be retrieved in the PHP script to construct and execute the SQL query for deletion. It's important to sanitize and validate the input parameters to prevent SQL injection attacks.
<?php
// Retrieve the parameter for the entry to be deleted
$entry_id = $_GET['entry_id'];
// Sanitize the input
$entry_id = filter_var($entry_id, FILTER_SANITIZE_NUMBER_INT);
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and execute the SQL query to delete the entry
$sql = "DELETE FROM table_name WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $entry_id);
$stmt->execute();
// Close the connection
$stmt->close();
$conn->close();
echo "Entry deleted successfully";
?>