in reply to multiple queries:

One thing I have come across is that you have to finish a statement before executing a new one.
Insert a $handle->finish(); before the }#ending while

It would help us, if you would provide the error message(s), perhaps one query has the wrong syntax.