Inserting Multiple Data from Input Boxes to MySQL db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ciaranholland
Forum Newbie
Posts: 6
Joined: Sat Feb 20, 2010 6:50 pm

Inserting Multiple Data from Input Boxes to MySQL db

Post by ciaranholland »

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:

Code: Select all

 
<?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!)

Code: Select all

 
<?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!
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: Inserting Multiple Data from Input Boxes to MySQL db

Post by mrcoffee »

By using "comments[]" as a form element name, PHP will generate the keys sequentially with nothing to identify which row they are associated with. If you used something like: <textarea name="comments[$row2['LogID']]">, then the $row variable set in your foreach statement on line 25 in savereport.php will be the LogID.

Hope that helps.
ciaranholland
Forum Newbie
Posts: 6
Joined: Sat Feb 20, 2010 6:50 pm

Re: Inserting Multiple Data from Input Boxes to MySQL db

Post by ciaranholland »

Thank you for the reply. I did this but can't get it working. Please excuse my ignorance. What will now be the code in the foreach loop?
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: Inserting Multiple Data from Input Boxes to MySQL db

Post by mrcoffee »

If you use name="comments[]", then $_POST['comments'] would look like this, for example:

Code: Select all

Array
(
    [0] => Comment for Log ID 1
    [1] => Comment for Log ID 4
    [2] => Comment for Log ID 10
)
Yet with name="comments[$LogID]", you get:

Code: Select all

 Array
(
    [1] => Comment for Log ID 1
    [4] => Comment for Log ID 4
    [10] => Comment for Log ID 10
) 
Your foreach construct is set up to retrieve both the key and value of the array, where $row is the key (which is now the LogID) and $com is the value.

Based on what I see, you then don't need line 28 (setting the LogID). Then in your MySQL statement, use $row instead of $id.
Post Reply