What best practices should be followed when updating database records with user input in PHP?

When updating database records with user input in PHP, it is important to properly sanitize and validate the user input to prevent SQL injection attacks and ensure data integrity. One best practice is to use prepared statements with parameterized queries to securely update the database records.

// Assuming $db is your database connection

// Sanitize and validate user input
$userInput = $_POST['userInput'];

// Prepare an update query using a prepared statement
$stmt = $db->prepare("UPDATE table_name SET column_name = :userInput WHERE id = :id");

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

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