What are some best practices for handling SQL syntax errors in PHP when updating database records?

When updating database records in PHP, it is important to handle SQL syntax errors gracefully to prevent any potential security vulnerabilities or data corruption. One best practice is to use try-catch blocks to catch any SQL exceptions and handle them appropriately, such as displaying an error message to the user or logging the error for debugging purposes.

try {
    // Your database connection code here

    // Your SQL update query here

    // Execute the query
    $stmt = $pdo->prepare("UPDATE table SET column = :value WHERE id = :id");
    $stmt->bindParam(':value', $value);
    $stmt->bindParam(':id', $id);
    $stmt->execute();

    // Success message
    echo "Record updated successfully";
} catch (PDOException $e) {
    // Error message
    echo "Error updating record: " . $e->getMessage();
}