How can the SQL queries in the PHP code snippet be simplified using a loop?

The SQL queries in the PHP code snippet can be simplified using a loop by storing the values in an array and then dynamically constructing the SQL query using a loop to iterate over the array elements. This approach reduces code duplication and makes the code more maintainable.

// Sample array of values
$values = ['value1', 'value2', 'value3'];

// Constructing the SQL query using a loop
$sql = "INSERT INTO table_name (column_name) VALUES ";
foreach ($values as $value) {
    $sql .= "('$value'),";
}
$sql = rtrim($sql, ','); // Remove the trailing comma

// Execute the SQL query
$result = mysqli_query($conn, $sql);

if ($result) {
    echo "Records inserted successfully.";
} else {
    echo "Error inserting records: " . mysqli_error($conn);
}