A complete header is needed to have a succesfull http response. You must analyze what your cgi script sends back and inspect headers to check they are complete and valid. If some error occurs (MySQL connection?) before headers are emitted you can have such errors too. You can test the cgi in command line (bypassing the server completely), see
Testing out a CGI script using the command line
Might be that the server put some hands in header composition and the results is bad. In this case you can, empirically speaking, try to play with
no-parse-header CGI mode and, eventually modifying the cgi code, tell the script to produce a complete and valid header.
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.