What best practices should be followed when updating database tables in PHP?

When updating database tables in PHP, it is important to use prepared statements to prevent SQL injection attacks and ensure data integrity. Additionally, always validate user input before updating the database to avoid errors and maintain security. Lastly, consider implementing error handling to gracefully handle any issues that may arise during the update process.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare the update statement
$stmt = $pdo->prepare("UPDATE users SET email = :email WHERE id = :id");

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

// Validate user input
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$id = intval($_POST['id']);

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

// Handle errors
if($stmt->rowCount() > 0) {
    echo "Update successful!";
} else {
    echo "Update failed";
}