You initialize $dbh using the first argument to the function, but you then overwrite that variable with a new connection for every loop iteration.
in reply to Problem with INSERT statement
Do one of these:
I would recommend #1 or #2, as #3 is only appropriate if you are connecting to dynamic sources. Very inefficient otherwise.
- Pass the handle in, do not disconnect in the subroutine
- Connect at the top of the subroutine, disconnect at the bottom of the subroutine
- Connect at the top of the loop, disconnect at the bottom of the loop