Jump to content

Inserting Multiple Data from Input Boxes to MySQL db


cholland

Recommended Posts

Hi All,

 

I'm new to PHP and seeing if any1 could help me with reading multiple text boxes and then updating my db with their values. In my first form it loops through rows in my db and creates an input box in the loop. I have the input boxes named 'comment[]' and their id being a primary key (not sure if this is correct). See my code showing the input boxes below:

<?php
//start the session 
session_start(); 

//check to make sure the session variable is registered 
if(isset($_SESSION['id']))
{


?>
<html>
<head>
<Title>View Report</Title>
</head>
<BODY>



<?php
include ("connect.php");
$id = $_GET['id'];

$query = "SELECT * FROM tblreport WHERE ReportID = $id";
$result = mysql_query($query);

echo "<form action=savereport.php?cmd=edit&id=$id method=POST>";


?>
<P><A href="report.php">Back</A></P>
<P><A href="TeacherHome.php">Home</A></P>


<?php
echo "<P><A href=printreport.php?id=$id>Printable Screen of Report</A></P>";
while($row = mysql_fetch_array($result)) {

echo "<FONT size=4 face=sans-serif>" . $row['ReportName'] . "</FONT>";
echo "<table border=1 cellSpacing=2 cellPadding=1>";
echo "<TR><TD><STRONG><FONT size=4>Student Name</TD><TD><STRONG><FONT size=4>" . $row['Heading'] . "</TD><TD><STRONG><FONT size=4>Additional Comments</TD></TR>";
$exam = $row['Exam'];


}

$query2 = "SELECT * FROM tblreportlog WHERE tblreportlog.reportID = $id";
$result2 = mysql_query($query2);



while($row2 = mysql_fetch_array($result2)) {

$StudentID = $row2['PupilID'];
$query3 = "SELECT * FROM tblPupil WHERE PupilID = $StudentID";
$result3 = mysql_query($query3);
while($row3 = mysql_fetch_array($result3)) {
	echo "<tr><td>" . $row3['FirstName'] . " " . $row3['SurName'] . "</td>";


//Need code in savereport.php to do this, aargh!!!

if ($exam == "1"){
echo "<td><input type=text name='result[]' value=\"".$row2['LogID']."\"></td>";
}
else{

$value = $row2['Value'];
if ($value == "1"){
	echo "<td><input type=checkbox name='box[]' value=\"".$row2['LogID']."\" CHECKED></td>";
} else {
	echo "<td><input type=checkbox name='box[]' value=\"".$row2['LogID']."\" ></td>";
}


}
echo "<td><input type=text name=comment[] id=id[" . $row2['LogID'] . "] size=20 value=" . $row2['Comment'] . "></td></TR>";
}

}


?>


<tr><td colspan =7 align=center><input type="submit" value="Save Report" name="Save">  <input type="submit" value="Back" name="cancel"></td></tr></table>





</form>
</body>
</html>

<?php
} 
else{ 

//the session variable isn't registered, send them back to the login page 
header( "Location: login1.html" ); 
} 

?> 

 

Then my code to process this is below. I tried a for each statement but I can't ge t it to work (again I'm not sure if this is the correct method of doing this?)

 

I need to pass the LogID value because i want to write the 'comment' value to the row that corresponds to it's LogID.

 

(sorry if i'm not making much sense!)

 

<?php

include ("connect.php");



$id = $_GET['id'];
$sqlupdate2="UPDATE tblReportLog SET `Value` = '0' WHERE ReportID='$id'";	
$resdel=mysql_query($sqlupdate2);

if ($_POST['Save'])
{

   if(isset($_POST['box'])){ 	

      $box = $_POST['box'];
							  	
	while (list ($key,$val) = @each ($box)) { 	
		$sqlupdate="UPDATE tblReportLog SET `Value` = '1' WHERE LogID='$val'";	
		$resdel=mysql_query($sqlupdate);	

	}															
   }
foreach($_POST['comment'] as $row=>$com) 
{ 
    $comment=mysql_real_escape_string($com); 
    $id=mysql_real_escape_string($_POST['id'][$row]); 
$sqlupdate2="UPDATE tblReportLog SET `Comment` = '$comment' WHERE LogID='$id'";	
$resdel2=mysql_query($sqlupdate2);

}
header('Location: report.php');
} else {
header('Location: report.php');
}

?>

 

Any help with this would be fantastic. I've been trying to figure this out for hours!

In my simplistic way of programming, I often break stuff up and make sure that parts are working correctly and then fit them together building up to the whole program.

 

Maybe it would help if you did this and also looked at the source generated to find out if you were creating the html that you expect.

 

It might be better to post a MySQL question separately if you cannot get that part to work because then it is separated into what works and what doesn't.

 

Hope that helps a bit anyway.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.