Can you show us the Do_SQL() subroutine? This is a key to your problem..
Other things I would suggest working on: not using $ENV{QUERY_STRING} at all if you can help it, putting a content-type header on your HTML output, using strict, and avoiding globals.