What are some common mistakes to avoid when updating data in PHP using MySQL?

One common mistake when updating data in PHP using MySQL is not properly sanitizing user input, which can lead to SQL injection attacks. To avoid this, always use prepared statements with parameterized queries to securely update data in the database.

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

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

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

// Sanitize user input
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$id = filter_var($_POST['id'], FILTER_SANITIZE_NUMBER_INT);

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