What am I doing wrong? The below code is intended to update a single selected record in my database but changes all records?


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

$apt = $_POST['apt'];
$query="SELECT * FROM payments Where apt='$apt'";
echo "<form action='#' method='post'><b><center>Present Report</center></b><br /><br />
<table border='1'>
<th colspan=2>num</th>
<th colspan=2>pay</th>
<th colspan=2>paid</th>

while($row = mysql_fetch_assoc($result))
echo "<tr>
<td><input type='text' name='dep' value='" . $row['dep'] . "'></td>
<td><input type='text' name='apt' value='" . $row['apt'] . "'></td>
<td><input type='text' name='name' value='" . $row['name'] . "'></td>
<td><input type='text' name='rent' value='" . $row['rent'] . "'></td>
<td><input type='text' name='prevbal' value='" . $row['prevbal'] . "'></td>
<td><input type='text' name='misc' value='" . $row['misc'] . "'></td>
<td><input type='text' name='rentdue' value='" . $row['rentdue'] . "'></td>
<td><input type='text' name='hudpay' value='" . $row['hudpay'] . "'></td>
<td><input type='text' name='tentpay' value='" . $row['tentpay'] . "'></td>
<td><input type='text' name='amtpaid' value='" . $row['amtpaid'] . "'></td>
<td><input type='text' name='datepaid' value='" . $row['datepaid'] . "'></td>
<td colspan='11' align='center' style='padding-top:20px;'><input type='submit' name='update' value='Update Record'></td>
echo "</table>
else{echo "No listing for appartment $apt.<br />Please select another.<br />";}
$sql = "UPDATE payments SET dep='".$_POST["dep"]."',apt='".$_POST["apt"]."',name='".$_POST["name"]."',rent='".$_POST["rent"]."',prevbal='".$_POST["prevbal"]."',misc='".$_POST["misc"]."',rentdue='".$_POST["rentdue"]."',hudpay='".$_POST["hudpay"]."',tentpay='".$_POST["tentpay"]."',amtpaid='".$_POST["amtpaid"]."',datepaid='".$_POST["datepaid"]."'";
mysql_query($sql) or die("Update query failed.");
echo "Record for appartment ".$_POST["apt"]." has been updated ...";
<form method="post" action="#">
<br />
<input type="text" name="apt"/>
<input type="submit" name="submit" value="Select Apartment for Payment"/>