What are the advantages of using INSERT INTO ... ON DUPLICATE KEY UPDATE over multiple UPDATE queries in PHP?

When updating MySQL records in PHP, using INSERT INTO ... ON DUPLICATE KEY UPDATE can be more efficient than running multiple UPDATE queries. This is because with a single query, you can insert a new record if it does not exist or update an existing record if it does. This can reduce the number of database queries and improve performance.

// Example of using INSERT INTO ... ON DUPLICATE KEY UPDATE in PHP
$query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2') ON DUPLICATE KEY UPDATE column2 = 'new_value'";
$result = mysqli_query($connection, $query);

if($result) {
    echo "Record inserted or updated successfully.";
} else {
    echo "Error: " . mysqli_error($connection);
}