Inserting Multiple Data from Input Boxes to MySQL db

Discussion in 'PHP' started by cholland, Feb 25, 2010.

  1. #1
    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" ); 
    } 
    
    ?> 
    
    Code (markup):
    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');
    }
    
    ?>
    
    Code (markup):
    Any help with this would be fantastic. I've been trying to figure this out for hours!
     
    cholland, Feb 25, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try using this name for your input fields:

    name = \"comment[".$row2['LogID']."]\"

    Then have a look at your $_POST data with
    print_r($_POST);
    PHP:
    and you can easuly adjust your insert code ;)
     
    Nyu, Feb 25, 2010 IP