How can one effectively separate and organize SET columns in a MySQL UPDATE query in PHP?

When updating SET columns in MySQL using PHP, it's important to separate and organize each column and its corresponding value properly. One way to do this is by using an associative array where the keys represent the column names and the values represent the new values for those columns. This approach helps to keep the code clean and organized, making it easier to update multiple columns in a single query.

// Define the columns and their new values in an associative array
$updates = array(
    'column1' => 'new_value1',
    'column2' => 'new_value2',
    'column3' => 'new_value3'
);

// Construct the SET clause for the UPDATE query
$setClause = '';
foreach ($updates as $column => $value) {
    $setClause .= "$column = '$value', ";
}
$setClause = rtrim($setClause, ', '); // Remove the trailing comma and space

// Construct the full UPDATE query
$query = "UPDATE table_name SET $setClause WHERE condition";

// Execute the query using your MySQL connection
$result = mysqli_query($connection, $query);

// Check for errors or handle the result as needed
if ($result) {
    echo "Update successful";
} else {
    echo "Error updating record: " . mysqli_error($connection);
}