What is the best practice for updating multiple rows in a MySQL database using PHP?

When updating multiple rows in a MySQL database using PHP, it is best practice to use a prepared statement with a loop to efficiently update each row. This helps prevent SQL injection and improves performance by reducing the number of queries sent to the database.

// Assuming $data is an array of rows to update with their respective values
$sql = "UPDATE table_name SET column1 = ?, column2 = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);

foreach ($data as $row) {
    $stmt->execute([$row['value1'], $row['value2'], $row['id']]);
}