I am querying a database to create a list of items against certain variables so that the user can input data relating to that item - I got this working fine, but from user feedback they now want to see what they entered last time before the enter new data.......
the code I have
$CName = $_POST['client'];
$SName = $_POST['section'];
$query = "SELECT CId FROM tblC, tblS WHERE tblC.CName='$CName'
AND tblS.SName='$SName'";
$result111 = mysql_query($query)
or die(mysql_error());
$row111 = mysql_fetch_array($result111);
$CRef = $row111["CId"];
$query = "SELECT ARef FROM tblS WHERE SName='$SName' AND CRef ='$CRef'";
$result11 = mysql_query($query)
or die(mysql_error());
$row11 = mysql_fetch_array($result11);
$ARef = $row11["ARef"];
$query = "SELECT SId FROM tblS WHERE SName='$SName' AND CRef = '$CRef' AND ARef ='$ARef'";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result))
$SRef = $row["SId"];
$query = "SELECT `CList` FROM `tblM` WHERE `SectionRef` = '$SRef' AND ARef ='$ARef' AND CRef ='$CRef'";
$result1 = mysql_query($query)
or die (mysql_error());
$row1 = mysql_fetch_array($result1);
$List = $row1["CList"];
$query = "SELECT (SNo) AS SNo, (CName) AS Location, (MId) AS MId,
(CList) AS CList FROM `tblM` WHERE SRef = '$SRef' AND ARef = '$ARef'";
$result2 = mysql_query($query)
or die(mysql_error());
if ($result2)
{
// this is the new bit of code just been added
$query= "SELECT MRef, Max(BW) As LastResultBW, Max(C) As LastResultC
FROM tblR
WHERE MRef = $result2[2]
AND CRef = $CRef
AND SRef = $SRef
AND ARef = $CRef
GROUP BY MRef, CRef, SRef, ARef";
$result3 = mysql_query($query) or die (mysql_error());
while ($row3 = mysql_fetch_array($result3))
//end of new code
//use result of this query in form below
echo' <form action="process.php" method="POST" >
<table align="centre" cellspacing="0" cellpadding="0">
<tr valign="top">
<td width="5"></td>
<td width="5"></td>
<td align="right" width="50"><b>s</b></td>
<td align="left" width="200"><b>  / Also Known As</b></td>
<td align="right" width="20"><b>Code</b></td>
<td align="right" width="20"></td>
<td align="left" width="200">Previous BW </td>
<td align="left" width="200"><b>BW </b></td>
<td align="left" width="200">Previous C </td>
<td align="left" width="200"><b>C</b></td>
</tr >';
while ($row2 = mysql_fetch_array($result2,MYSQL_NUM))
{
echo "<td width=\"5\" align=\"left\"><input type=\"hidden\" name=\"CList\" value=\"{$row2[4]}\"></td>
<td width=\"5\" align=\"left\"><input type=\"hidden\" name=\"SRef\" value=\"$SRef\"></td>";
if ($row2[3] == 1)
{
echo "<td width=\"50\" align=\"left\"> {$row2[0]}</td>
<td width=\"200\" align=\"left\"> {$row2[1]}</td>
<td width=\"20\" align=\"right\"> {$row2[2]}</td>
<td width=\"20\" align=\"left\"> {$row2[4]} <input type=\"text\" name=\"MId[]\" size=\"3\"></td>
<td width=\"200\" align=\"left\"> {$row3[1]} </td>
<td width=\"200\" align=\"left\"> {$row2[5]} <input type=\"text\" name=\"BW[]\"></td>
<td width=\"200\" align=\"left\"><input type=\"hidden\" name=\"C[]\" value\"{NULL}\"></td>";
}
else
{
echo "<td width=\"50\" align=\"left\"> {$row2[0]}</td>
<td width=\"200\" align=\"left\">  / {$row2[1]}</td>
<td width=\"20\" align=\"right\"> {$row2[2]}</td>
<td width=\"20\" align=\"left\"> {$row2[4]} <input type=\"text\" name=\"MId[]\" size=\"3\"></td>
<td width=\"200\" align=\"left\"> {$row3[1]} </td>
<td width=\"200\" align=\"left\"> {$row2[5]} <input type=\"text\" name=\"BW[]\"> </td>
<td width=\"200\" align=\"left\"> {$row3[2]} </td>
<td width=\"200\" align=\"left\"> {$row2[6]} <input type=\"text\" name=\"C[]\"></td>";
}
echo "</font></tr>\n";
} // close while loop
} //close main if
echo '</table>';
echo "<br><p align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Submit\"> ";
echo '</form>';
everything was working fine but when I added this new bit of code I now get the error message
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND CRef = 354 AND SRef = 135 AND CRef = 2 GROUP BY MRef, ' at line 4
Does anyone know where I am going wrong - many thanks