How can user input be securely handled in PHP to prevent SQL injections when deleting data from a database?

To securely handle user input in PHP to prevent SQL injections when deleting data from a database, you should use prepared statements with parameterized queries. This method separates the SQL query logic from the user input, preventing malicious SQL code from being executed. By binding parameters to the query, the input is treated as data rather than executable code.

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

// Prepare a SQL query with a parameterized statement
$stmt = $pdo->prepare("DELETE FROM table WHERE id = :id");

// Bind the user input to the parameter
$stmt->bindParam(':id', $_POST['id']);

// Execute the query
$stmt->execute();