What are some best practices for handling database operations like deleting records in PHP?

When handling database operations like deleting records in PHP, it is important to sanitize user input to prevent SQL injection attacks. Additionally, always use prepared statements to execute SQL queries, as this helps prevent SQL injection and improves performance. Lastly, consider implementing error handling to gracefully handle any database-related errors that may occur.

// Example of deleting a record from a database table using PDO and prepared statements

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

// Sanitize user input (assuming $id is the ID of the record to be deleted)
$id = filter_var($_POST['id'], FILTER_SANITIZE_NUMBER_INT);

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

// Bind parameters
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

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