I have a user input form, which adds new records into the database, However I am getting a lot of errors (users not watching what they are typing) which is skewing my reports. I want to add some safety checks into my code so that I can check the validity of the user input before is goes into the database.
For example, last months figure for 'london' was 1002, I am expecting that this months figure is going to be higher, so I want to say, last month = A, this month = X - query = is X greater than A ? if so add the new record, if not throw up an error message.......
On the user input form I have added 'last months data' as a visual prompt for the user and also included the data as a hidden filed to carry across to the process page - but when I query it in the process form page, I cannot get it to work.
One of the problems I think might be due to the fact that on the user input form I have numerous lines to record different information for different locations so the form looks something like;
location code previous month this month
london text box 1002 text box
xxxxxx text box 5114 text box
xxxxxx text box 1222 text box
xxxxxx text box 4562 text box
so I am doing a foreach query so that I look line by line at each location to verify the records.........
I would be very grateful if someone could take a look and see where I am going wrong - and hopefully put me back on track.
Code for User Input Form
require_once ('../mysql_connect.php'); //connect to the database
// 2 variables taken from previous page
$CoName = $_POST['client'];
$SName = $_POST['S'];
//query goes here to get this variable
$CRef = $row111["CId"];
//query goes here to get this variable
$ConRef = $row11["ConRef"];
//query goes here to get this variable
$SRef = $row["SId"];
//query goes here to get this variable
$List = $row1["CList"];
//list locations
$query = "SELECT (SNo) AS SNo, (CName) AS Location, (MId) AS MId,
(CList) AS CList FROM `tblM` WHERE SRef = '$SRef' AND ConRef = '$ConRef'";
$result2 = mysql_query($query)
or die(mysql_error());
if ($result2)
{
echo' <form action="process.php" method="POST" > <FONT FACE="arial" SIZE="1">
<table align="centre" cellspacing="0" cellpadding="0">
<tr valign="top">
<td width="2"></td>
<td width="2"></td>
<td align="right" width="50"><b>Location</b></td>
<td align="right" width="30"><b>Code</b></td>
<td width=\"2\" align=\"left\"></td>
<td align="right" width="170">BW - <br>Previous Month </td>
<td align="left" width="200"><b>BW -<br> This Month</b></td>
</tr >';
while ($row2 = mysql_fetch_array($result2,MYSQL_NUM))
{
$query= "SELECT (MRef) AS MRef, Max(data) As LastResult
FROM tblResults
WHERE MRef = '".$row2[2]."'
AND CRef = $CRef
AND SRef = $SRef
AND ConRef = $ConRef
GROUP BY MRef, CRef, SRef, ConRef";
$result3 = mysql_query($query) or die (mysql_error());
$row3 = mysql_fetch_array($result3);
echo "<td width=\"2\" align=\"left\"><input type=\"hidden\" name=\"CList\" value=\"{$row2[3]}\"></td>
<td width=\"2\" align=\"left\"><input type=\"hidden\" name=\"SRef\" value=\"$SRef\"></td>";
echo "
<td width=\"50\" align=\"left\"> {$row2[0]}</td>
<td width=\"30\" align=\"left\"> <input type=\"text\" name=\"MId[]\" size=\"3\"></td>
<td width=\"170\" align=\"right\"> $row3[1]   </td>
<td width=\"2\" align=\"left\"><input type=\"hidden\" name=\"PreviousMonth\" value=\"{$row3[1]}\"></td>
<td width=\"200\" align=\"left\"> <input type=\"text\" name=\"data[]\"></td>
";
}
echo "</font></tr>\n";
} // close while loop
} //close main if
echo '</font></table>';
echo "<br><p align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Submit\"> ";
echo '</form>';
Code For processing the form
require_once ('../mysql_connect.php'); //connect to the database
$SRef = $_POST['SRef'];
$query = "SELECT ConRef, CRef FROM tblS WHERE SId = '$SRef'";
$result101 = mysql_query($query) or die (mysql_error());
$row101 = mysql_fetch_array($result101);
$CoRef = $row101["ConRef"];
$ClRef = $row101["CRef"];
// define all the machines for this section for this input
$query = "SELECT MId FROM tblM, tblUser
WHERE tblM.CRef = '$ClRef'
AND tblM.SRef = '$SRef'
AND tblUser.CRef = tblM.CRef";
$result1001 = mysql_query($query) or die (mysql_error());
while ($row1001 = mysql_fetch_array($result1001))
$M[] = $row1001["MId"];
// check this months data is greater than previous
if (is_array($_POST['MId']) && count($_POST['MId']) >0)
{
foreach (($_POST['MId']) as $value)
{
if (($_POST['data']) < ($_POST['PreviousMonth']))
{
echo "<br><br>" .
"<font face=\"Arial, Helvetica, sans-serif\">" .
"Sorry, it appears that this months data </b> " .
"is lower than that entered last month.<br>Please kindly return to the " .
"input form, and re-enter this data. " .
"<br>Thank you" .
"</font>";
}
} //close foreach
} //close if
// insert the record
else
{ // code to insert the rcords goes here
}