Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not INSERTing all results into database

Status
Not open for further replies.

ejwf

Technical User
Joined
Mar 31, 2004
Messages
2
Location
GB
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. With the help of these forums 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:
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:
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())
"; 
$result2 = 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 !!!
 
Outputting the variable values is a start, but doesn't test enough. When you're getting strange results from a database interaction, the cause is nearly always a badly-formed query string passed to the server.

Output the query to the browser and examine it. Does it look right?

Copy-and-paste the query to your preferred database admin tool. Does the query work as expected there?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top