I don't know why I get the message "unexpected $end" at the last line of code - can someone help?
<?php
mysql_connect(localhost,root,"");
mysql_select_db(test) or die( "Unable to select database");
if(!empty($_POST["submit"]))
{
$apt = $_POST['apt'];
$query="SELECT * FROM payments Where apt='$apt'";
$result=mysql_query($query);
if(mysql_num_rows($result))
?>
<html><head><script type="text/javascript">
if ($late = "L") {$rentdue = $rentdue + 10;}
var excess = $amtpaid - $rentdue;
var totOwed = $rentdue + $prevbal + $secdep + $damage + $latechg + $courtcost +
$nsf - $hudpay;
if ($amtpaid >= totOwed) { $prevbal = totOwed - $amtpaid;
excess = 0 ; $secdep = 0 ; $damage = 0 ; $latechg = 0 ; $courtcost = 0 ; $nsf = 0; }
if (excess < $prevbal && $amtpaid > $rentdue) { $prevbal = $prevbal - excess; excess = 0}
if (excess >= $prevbal) { excess = excess - $prevbal; $prevbal = 0 ; }
if (excess < $secdep && $amtpaid > $rentdue) { $secdep = $secdep - excess; excess = 0}
if (excess >= $secdep) { excess = excess - $secdep; $secdep = 0 ; }
if (excess < $damage && $amtpaid > $rentdue) { $damage = $damage - excess; excess = 0}
if (excess >= $damage) { excess = excess - $damage; $damage = 0 ; }
if (excess < $latechg && $amtpaid > $rentdue) { $latechg = $latechg - excess; excess = 0}
if (excess >= $latechg) { excess = excess - $latechg; $latechg = 0 ; }
if (excess < $courtcost && $amtpaid > $rentdue) { $courtcost = $courtcost - excess; excess = 0}
if (excess >= $courtcost) { excess = excess - $courtcost; $courtcost = 0 ; }
if (excess < $nsf && $amtpaid > $rentdue) { $nsf = $nsf - excess; excess = 0}
if (excess >= $nsf) { excess = excess - $nsf; $nsf = 0 ; }
}
</script>
<?php>
$sql = "UPDATE payments SET
amtpaid = '0', prevbal, tentpay = '0', datepaid = ' ', late = ' ', damage, courtcost, nsf, latechg, secdep, comments = ' ', paidsum = '0'
WHERE prevbal + rentdue = amtpaid OR late = 'L');
mysql_query($sql) or die("Update query failed.");
echo "Records have been updated";
?>
</head></html>