The problem is, is that as soon as information is sent from the sever to the browser, the headers come along with it. Here is the process of executing a PHP script:
PC --(request webpage)--> Sever - outputs html
Sever --(goes to php parser change php into html)-->PHP parser - is told to output stuff
PHP parser --(Sends HTML to SEVER)--> Sever
Sever --(Sends HTML to PC)--> PC
(The page is not fully loaded, so the process repeats)
When the page sends information from the sever to the PC the header information comes with it. In your script you telling it to resend the header information multiple times and therefore it is causing an error, as it can't. Instead you should send the headers first and then the remaining script.
Consider this script:
<?php
echo "Hello";
echo "<head><meta name = "blah"></head>";
?>
It will report an error because you are resending the headers.
However this would work:
<?php
echo "<head><meta name = "blah"></head>";
echo "Hello";
?>
Here the headers are only being sent once. I hope that helps.
*note, this is what I am lead to believe. If I am incorrect please put me right*