Can't say for sure, but I have a negative premonition. Notice that $ext, $secret, $macaddress, and $templatename are now all arrays, containing multiple values, but you are only running one query.
Modify your code to execute three queries, one for each set of values.
for (var $i = 0; i++; i...