I am working on a user entry form that querys the mysql database, and creates the form based on the results of the query.
Then I want to only insert records into the database where the user has completed the row on the form and ignore the rows where there is no input.
The code I have so far creates the user entry form OK, but inserts all the rows into the database regardless of whether there is user entered data in there or not. For the rows where there is no user entry I get '0' records in the database (which causes me problems later down the line).
Can anyone suggest a way of cleaning this up so I only get a record inserted when the user puts data in the form - TIA
code I have so far;
to create the form
$query = "SELECT (Type) AS Type, (No) AS No, (Name) AS Location, (Id) AS Id, (List) AS List FROM `tbl1` WHERE Ref = '$Ref' ";
//variable $Ref set earlier in the code
$result2 = mysql_query($query)
or die(mysql_error());
if ($result2)
{
echo' <form action="process.php" method="POST">
<table align="centre" cellspacing="0" cellpadding="0">
<tr>
<td align="left"></td>
<td align="left"></td>
<td align="left"><b>Ref</b></td>
<td align="left"><b>Number</b></td>
<td align="left"><b>Location</b></td>
<td align="right"><b>Code</b></td>
<td align="left"></td>
<td align="left"><b></b></td>
</tr>';
while ($row2 = mysql_fetch_array($result2,MYSQL_NUM))
{
echo "<td width=\"10\" align=\"left\"><input type='hidden' name='Ref' value='$Ref'></td>
<td width=\"10\" align=\"left\"><input type=\"hidden\" name=\"List\" value\"{$row2[4]}\"></td>
<td width=\"70\" align=\"left\"> {$row2[0]}</td>
<td width=\"120\" align=\"left\"> {$row2[1]}</td>
<td width=\"200\" align=\"left\"> {$row2[2]}</td>
<td width=\"30\" align=\"right\"> {$row2[3]}</td>";
if ($row2[4] == 1)
{
echo "<td width=\"10\" align=\"left\"> {$row2[7]} <input type=\"text\" name=\"Id[]\" size=\"3\"></td>
<td width=\"200\" align=\"left\"> {$row2[5]} Size <input type=\"text\" name=\"Size[]\"></td>
<td width=\"300\" align=\"left\"><input type=\"hidden\" name=\"Colour[]\" value\"{NULL}\"></td>";
}
else
{
echo "<td width=\"1\" align=\"left\"> {$row2[7]} <input type=\"text\" name=\"[]\" size=\"3\"></td>
<td width=\"200\" align=\"left\"> {$row2[5]} Size <input type=\"text\" name=\"Size[]\"> </td>
<td width=\"300\" align=\"left\"> {$row2[6]} Colour <input type=\"text\" name=\"Colour[]\"></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>';
to insert the data
// check to see if id has been submitted
if(is_array($_POST['Id']) && count($_POST['Id'])>0)
{
foreach($_POST['Id'] as $value)
{
if (empty($_POST['Id']) && !empty($_POST['Colour']))
{
$empty=false;
}
elseif (empty($_POST['Id']) && empty($_POST['Colour']))
{
$empty=true;
}
} // close foreach
} // close if
// if ref is empty give error message
if($empty)
{
echo"<br><br><br><br><font face=\"Arial, Helvetica, sans-serif\">Sorry, it appears that the <b>Code</b>
is missing.<br><br>Please click on the back arrown of your browser to return to the
input form, and re-enter this code. <br>Thank you</font>";
}
// if ref is not empty process form
if(!$empty)
{
// sets date into correct format to match database
$today = date('YmdHis');
$Size = $_POST['Size'];
if ($Size IS NOT NULL)
{
$arr_size = count($_POST['Size']);
for ($i=0;$i< $arr_size;$i++)
{
$Size = strip_tags(trim($_POST['Size'][$i]));
$Colour = strip_tags(trim($_POST['Colour'][$i]));
$Id = ($_POST['Id'][$i]);
$query = "INSERT INTO tblResults (DateRef,Ref,SRef,CRef,Size,Colour,By)
VALUES ('$DateRef','$Id','$SRef','$CRef','$Size','$Colour','$UserId')";
$result = mysql_query($query)
or die(mysql_error());
} //close for
} //close if
echo "<p></p><p></p><p></p>
<p><div align=\"center\"><font face=\"Arial, Helvetica, sans-serif\" size=\"3\"><b><font color=\"#FF0000\">
Thank you $username, <br> <br> your information has been added<br> and will be processed accordingly.
</font></b></font></div></p>";