Jump to content

foreach($_POST['x'] as $key => $value)


Recommended Posts

I have a script that is currently not working.  I have done several searches but can't figure this out.

 

I have a table _tl_session_grades I am trying to update records in.  It already contains data for the Session_ID, SOMS_KEY, UID , Group_ID, IRAT_Grade and Academic_Year.

 

Session_ID   	int(11)  
SOMS_KEY  	int(11) 	
UID  	varchar(9) 	
Group_ID  	int(11) 	
IRAT_Grade  	int(4) 	  	
GRAT_Grade  	int(4) 	  	  
AppEx_Grade  	int(4) 	  	  	
Academic_Year  	varchar(5)

 

I need to update the GRAT_Grade.  I have about 16 Groups (Group_ID) that I have a form for and place a grade into a text box.

 

 

echo "<form method='post' action='".$admin_file.".php'>\n";
echo "<input type='hidden' name='op' value='TLSessionGratGradesInsert'>\n";
echo "<table width='100%' border='1' cellspacing='0' cellpadding='2'>\n";
echo "<tr><td colspan='3' width='100%' bgcolor='$bgcolor2'><nobr><b>TL GRAT Session Name: $Session_Name for Course $Course_Name</b></nobr></td></tr>\n";
echo "<tr><td colspan=2 width='100%'><nobr>Year $Course_Year Groups</td></tr>\n";
echo "</table>\n";
echo "<br />\n";
echo "<table width='100%' align='center' border='1' cellspacing='0' cellpadding='2'>\n";
echo "<tr><td align='center' bgcolor='$bgcolor2' width='5%'><b>Group ID</b></td><td align='left' bgcolor='$bgcolor2' width='50%'><b>Group Name</b></td><td align ='center' bgcolor='$bgcolor2' width='20%'><b>GRAT Grade</b></td></tr>\n";
$groupYearlist = $db->sql_query("SELECT * FROM ".$prefix."_tl_groups WHERE Group_Year = $Course_Year");
while ($row = $db->sql_fetchrow($groupYearlist)) {
	$Group_ID = $row['Group_ID'];
	$Group_Name = $row['Group_Name'];		

	if (!$groupYearlist) {
	echo("<p>Error performing query: " . mysql_error() . "</p>");
	exit();   
	}	

  //<!-- BEGIN Group Row -->"
   echo "<tr><td align=\"center\">$Group_ID</td>"  
  . " <td align=\"left\">$Group_Name</td>"
  . " <td><input type='text' name='GRAT_Grade[]' size='3' maxlength='3'></td>"  
  . "</tr>";  
  }
  //-- END Group Row -->" 
echo "<tr><td colspan='3' align='center'><input type='submit' value='Add Grade(s)'></td></tr>\n";
echo "<input type='hidden' name='Session_ID' value='$Session_ID'>\n";
echo "<input type='hidden' name='Group_ID' value='$Group_ID'>\n";
echo "</form>\n";
echo "</table>\n";

 

All 16 groups grade need to be updated, but I am only updating the last Group_ID from the group table.  Each group_ID is not getting its GRAT_Grade updated.

 

Here is the relevant code from the TLSessionGratGradesInsert page.

 

$Session_ID = $_POST['Session_ID'];
$Group_ID = $_POST['Group_ID'];	
foreach($_POST['GRAT_Grade'] as $key => $value) { 
	$sql = $db->sql_query("UPDATE ".$prefix."_tl_session_grades SET GRAT_Grade = '$GRAT_Grade' WHERE Group_ID = '$Group_ID' AND Session_ID = '$Session_ID'") or die('Error, query failed. ' . mysql_error());
}

 

I understand that GRAT_Grade needs to be an array, but I think I have two values Group_ID and GRAT_Grade I am keying on.  I can't find the proper loop I need.

Link to comment
https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/
Share on other sites

The first thing to do in these cases is look at your $_POST variable:

echo '<pre style="text-align: left;">' . print_r($_POST, true) . '</pre>';

Is everything in $_POST the way you expected it to be?  If it isn't change your form so that it is or change your processing code so that it works with what the form is giving you.

 

I'll wager that the GroupID is different for each row, so to fix your problem you might change this line:

  . " <td><input type='text' name='GRAT_Grade[]' size='3' maxlength='3'></td>"

to

  . " <td><input type='text' name='GRAT_Grade[{$Group_ID}]' size='3' maxlength='3'></td>"

 

Then when you perform this loop:

foreach($_POST['GRAT_Grade'] as $key => $value) { 

$key will actually be the Group_ID of the corresponding group.

hmmm interesting.

 

 

the page returned this.

 

Array
(
    [op] => TLSessionGratGradesInsert
    [GRAT_Grade] => Array
        (
            [0] => 90
            [1] => 100
            [2] => 90
            [3] => 70
            [4] => 70
            [5] => 80
            [6] => 85
            [7] => 90
            [8] => 100
            [9] => 90
            [10] => 95
            [11] => 90
        )

    [session_ID] => 43
    [Group_ID] => 16
)

 

Looks like I am only getting the last Group_ID.

That because your hidden form fields (which carry the group_id and session_id are out side of your loop herE:

$groupYearlist = $db->sql_query("SELECT * FROM ".$prefix."_tl_groups WHERE Group_Year = $Course_Year");
while ($row = $db->sql_fetchrow($groupYearlist)) {
	$Group_ID = $row['Group_ID'];
	$Group_Name = $row['Group_Name'];		

	if (!$groupYearlist) {
	echo("<p>Error performing query: " . mysql_error() . "</p>");
	exit();   
	}	

  //<!-- BEGIN Group Row -->"
   echo "<tr><td align=\"center\">$Group_ID</td>"  
  . " <td align=\"left\">$Group_Name</td>"
  . " <td><input type='text' name='GRAT_Grade[]' size='3' maxlength='3'></td>"  
  . "</tr>";  
  }
  //-- END Group Row -->" 
echo "<tr><td colspan='3' align='center'><input type='submit' value='Add Grade(s)'></td></tr>\n";
echo "<input type='hidden' name='Session_ID' value='$Session_ID'>\n";
echo "<input type='hidden' name='Group_ID' value='$Group_ID'>\n";

 

The last two lines in the above code need to be within your while loop.

I am now getting this:

 

Array
(
    [op] => TLSessionGratGradesInsert
    [GRAT_Grade] => Array
        (
            [1] => 90
            [4] => 90
            [7] => 80
            [8] => 70
            [9] => 95
            [10] => 85
            [11] => 90
            [12] => 100
            [13] => 100
            [14] => 90
            [15] => 80
            [16] => 70
        )

    [session_ID] => 43
)

 

Which is correct.

 

From this code:

 

$groupYearlist = $db->sql_query("SELECT * FROM ".$prefix."_tl_groups WHERE Group_Year = $Course_Year");
while ($row = $db->sql_fetchrow($groupYearlist)) {
	$Group_ID = $row['Group_ID'];
	$Group_Name = $row['Group_Name'];		

	if (!$groupYearlist) {
	echo("<p>Error performing query: " . mysql_error() . "</p>");
	exit();   
	}	

  //<!-- BEGIN Group Row -->"
   echo "<tr><td align=\"center\">$Group_ID</td>"  
  . " <td align=\"left\">$Group_Name</td>"  
  . " <td><input type='text' name='GRAT_Grade[{$Group_ID}]' size='3' maxlength='3'></td>"
  . "</tr>";   
  }
  //-- END Group Row -->" 
echo "<tr><td colspan='3' align='center'><input type='submit' value='Add Grade(s)'></td></tr>\n"; 
echo "</form>\n";
echo "</table>\n";

 

But I am having  trouble updating the table.

 

$Session_ID = $_GET['Session_ID'];
$Group_ID = $_POST['Group_ID'];	
foreach($_POST['GRAT_Grade'] as $Key => $value) { 
	$sql = $db->sql_query("UPDATE ".$prefix."_tl_session_grades SET GRAT_Grade = '$GRAT_Grade' WHERE Group_ID = '$Group_ID' AND Session_ID = '$Session_ID'") or die('Error, query failed. ' . mysql_error());
}

 

If $Key is the Group_ID of the corresponding group do I need WHERE Group_ID = '$Key' ?

If $Key is the Group_ID of the corresponding group do I need WHERE Group_ID = '$Key' ?

That would make logical sense.

 

And drop your hidden GroupID field, it's useless.

 

Tell me, is the SessionID different for each row as well?

Is this solved?  Or are you still having trouble?

 

Values are still not being written to each record.

 

 

I hard coded a Session_ID = 43 to see if the values was not passed but so far no good.

 

Please use code tags. -- roopurt18

$Session_ID = $_POST['Session_ID'];
$Group_ID = $_POST['Group_ID'];	
foreach($_POST['GRAT_Grade'] as $Key => $value) { 
	$sql = $db->sql_query("UPDATE ".$prefix."_tl_session_grades SET GRAT_Grade = '$GRAT_Grade' WHERE Group_ID = '$Key' AND Session_ID = 43") or die('Error, query failed. ' . mysql_error());
}

 

My array still looks good but can't seem to be able to extract what I need to update the table with.

 

Array
(
    [op] => TLSessionGratGradesInsert
    [GRAT_Grade] => Array
        (
            [1] => 100
            [4] => 90
            [7] => 80
            [8] => 85
            [9] => 90
            [10] => 95
            [11] => 100
            [12] => 90
            [13] => 85
            [14] => 100
            [15] => 90
            [16] => 90
        )

    [session_ID] => 43
)[\code]

SET GRAT_Grade = '$GRAT_Grade'

Why are you using $GRAT_Grade in the query?  Wouldn't you want to use the value from $_POST, which in the loop is $value?

 

I see you have an mysql_error() call in there; is it printing anything?

 

I generally do queries in two steps.  I set the query into a variable, usually named $sql.  Then I call my query function.

 

Try breaking your code out like this:

        $sql = "
          UPDATE ".$prefix."_tl_session_grades 
          SET GRAT_Grade = '$GRAT_Grade' 
          WHERE Group_ID = '$Key' AND Session_ID = 43
        ";
        // Now you can echo your queries and make sure they're correct
        echo '<pre style="text-align: left;">' . print_r($sql, true) . '</pre>';
        $sql = $db->sql_query($sql) or die('Error, query failed. ' . mysql_error());

 

That may give you more insight as to why it's not working.

 

Lastly, I want to give you a word of caution when using die() with mysql_error().  IMO, you should strive to not have any calls to die() in your final product.  They are perfectly fine for testing and debugging, but your final program should be able to gracefully recover from errors.  The same thing goes with echo'ing the results of mysql_error() or sql statements; it is fine for debugging, but make sure you remove them in the final product.

 

You always want to give away as little as possible in terms of how your program works.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.