I strongly recommend that you not use if():, else:, or endif;, especially since you use the much clearer if() {} else {} at other points in your code. stick with the {} version. I'm pretty sure the way you have it set up right now that the opening if only affects the $companyname=$_POST['companyname']; line, a bit like doing
if(isset($_POST['companyname']))
$companyname = $_POST['companyname'];
which is the same as
if(isset($_POST['companyname'])) {
$companyname = $_POST['companyname'];
}