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>

Recommended Answers

All 12 Replies

Make these corrections:
1. On line 2, you wrote

mysql_connect(localhost,root,"");

instead of

mysql_connect("localhost","root","");

2. On line 3, you wrote

mysql_select_db(test) or die( "Unable to select database");

instead of

mysql_select_db("test") or die( "Unable to select database");

3. On line 9, you wrote

if(mysql_num_rows($result))

instead of

if(mysql_num_rows($result))
{

4. On line 32, you wrote

<?php>

instead of

<?php

5. Your query on line 33 to 35 you wrote

$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');

istead of

$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')";

6.Finally, you should add

}
}

after line 38.

After the correction your code should look it this:

<?php
mysql_connect("localhost","root","man");
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";
}
}
?>

I mistakenly deleted the last line of your code

<head></html>

You can still add it to the last line.

thanks for the help but it still does the same

The short message would be: Check your string quoting.

Next, your update query is invalid.
And then, check your javascript, because you mix js and php variables. Not possible like this. Also a } too much I think.

If you copied the code that I posted, then the only thing that should result in an error should be on line 2. On that line, you wrote

mysql_connect("localhost","root","");

but I changed it to

mysql_connect("localhost","root","man");

when I was testing so simply reverse it. On the other hand, if you've changed that and you no longer get the error before but cannot get what you expect from you code, then I think you should check your queries and your javascript codes. Mixing php and javascript codes can make it difficult for even you to understand your own code. A better way would be for you to put all you javascript functions in an external file and then call them where appropriate.

If your javascript file is in the same folder as your php script, simply add the line in between your head tags like this

<head>
<script language = "javascript" type = "text/javascript" src = "javascript.js"></script>
</head>

Now you can call the functions as if they are embedded in page by their names and passing them the necessary parameters.

thanks a lot. that'll make it easier

pritaeas, could you please tell me how I should be stating my variables? That has been my doubt all along. I don't know how to assign values in the javascript to the php

As you may know Javascript is a client-side scripting whereas PHP is a server-side scripting language. That's they are two different languages and cannot share a variable.

As you may know Javascript is a client-side scripting whereas PHP is a server-side scripting language. That's they are two different languages and cannot share a variable.

so the calcs must be done in php?

Exactly, that is what you should do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.