Not INSERTing all of the results
Posted: Sat Apr 10, 2004 6:53 am
Can anyone help please......
on page 1. I have got a form that shows the result of a query and also has text boxes for users to add information. As a result of the query it is more than likely that there will be multiple rows to this table.
on page 2. (process.php) I am trying to code something up to take the results of the form and put them into a table in my database. I have managed to get the code to put in all of the multiple rows into the database so the database creates a record for each of the rows returned by the query, but it doesn't put in all of the results into each of these records.
code for page 1
code for page 2.
Of the information I want to add.... MachineID / Size / Colour / LoadedBy (username) / LoadedWhen (NOW()) ........ it is adding Size and LoadedWhen, but not the others.
I have echo'd all of these variables to check where it might be going wrong.
It will echo $username OK but it isn't putting the information into the database.
On the echo for Colour and MachineId it isn't outputting anything, so I know there is something wrong in the way it is taking the information from the form, but I don't know what, or what to do to put it right.
Any help and advice will be gratefully received !!!
on page 1. I have got a form that shows the result of a query and also has text boxes for users to add information. As a result of the query it is more than likely that there will be multiple rows to this table.
on page 2. (process.php) I am trying to code something up to take the results of the form and put them into a table in my database. I have managed to get the code to put in all of the multiple rows into the database so the database creates a record for each of the rows returned by the query, but it doesn't put in all of the results into each of these records.
code for page 1
Code: Select all
session_start ();
$_SESSION['user_id'] = $row[0];
$_SESSION['username'] = $row[1];
require_once ('../connect.php'); //connect to the database
include ('header.php'); //show header
$query = "SELECT `SectionRef` FROM `tblUser` WHERE `CLogIn` = '$username'";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
$SectionRef = $row["SectionRef"];
$query = "SELECT (Model) AS Model, (SerialNo) AS SerialNo, (CommonName) AS Location, (MachineId) AS MachineId FROM `tblMachine` WHERE SectionRef = '$SectionRef' ";
$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"><b>Model</b></td>
<td align="left"><b>Serial Number</b></td>
<td align="left"><b>Location</b></td>
<td align="left"></td>
</tr>';
while ($row2 = mysql_fetch_array($result2,MYSQL_NUM))
{
echo "<tr><font face="Arial, Helvetica, sans-serif">
<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="20" align="left"> <input type="hidden" name="MachineId" value"{$row2[3]}"> </td>
<td width="200" align="left"> {$row2[4]} <input type="text" name="Size[]"> </td>
<td width="300" align="left"> {$row2[5]} <input type="text" name="Colour[]"></td><br>
</font></tr>\n";
} // close while loop
} // close if
echo '</table>';
echo "<br><p align="center"><input type="submit" name="submit" value="Submit"> ";
echo '</form>';code for page 2.
Code: Select all
session_start ();
require_once ('../connect.php'); //connect to the database
include ('header.php'); //show header
echo "machineid = '$MachineId' <br>
Size = '$Size' <br>
Colour = '$Colour' <br>
Username = '$username' ";
if (isset($_POST['Size']))
{
$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]));
$MachineId = strip_tags(trim($_POST[MachineId][$i]));
$query = "INSERT INTO tblResults (MachineRef,Size,Colour,LoadedBy,LoadedWhen)
VALUES ('$MachineId','$Size','$Colour','$username',NOW())
";
mysql_query($query)
or die(mysql_error());
}
}
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>"Of the information I want to add.... MachineID / Size / Colour / LoadedBy (username) / LoadedWhen (NOW()) ........ it is adding Size and LoadedWhen, but not the others.
I have echo'd all of these variables to check where it might be going wrong.
It will echo $username OK but it isn't putting the information into the database.
On the echo for Colour and MachineId it isn't outputting anything, so I know there is something wrong in the way it is taking the information from the form, but I don't know what, or what to do to put it right.
Any help and advice will be gratefully received !!!