Why is the insert statement being overwritten in the foreach loop, causing only the last value to be inserted?

The issue occurs because the insert statement is being overwritten in each iteration of the foreach loop, causing only the last value to be inserted into the database. To solve this problem, you need to concatenate or append each insert statement within the loop instead of overwriting it.

// Fix for the issue of insert statement being overwritten in foreach loop
$insertQuery = "INSERT INTO table_name (column_name) VALUES ";

foreach ($array as $value) {
    $insertQuery .= "('$value'), ";
}

// Remove the last comma and space
$insertQuery = rtrim($insertQuery, ', ');

// Execute the insert query
// mysqli_query($connection, $insertQuery);