Donovan Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/ Share on other sites More sharing options...
roopurt18 Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-432867 Share on other sites More sharing options...
Donovan Posted January 7, 2008 Author Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-432869 Share on other sites More sharing options...
wildteen88 Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-432871 Share on other sites More sharing options...
Donovan Posted January 7, 2008 Author Share Posted January 7, 2008 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' ? Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-432940 Share on other sites More sharing options...
roopurt18 Posted January 7, 2008 Share Posted January 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-432995 Share on other sites More sharing options...
Donovan Posted January 8, 2008 Author Share Posted January 8, 2008 Will do and no the Session_ID is the same. Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-433226 Share on other sites More sharing options...
roopurt18 Posted January 8, 2008 Share Posted January 8, 2008 Is this solved? Or are you still having trouble? Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-433297 Share on other sites More sharing options...
Donovan Posted January 8, 2008 Author Share Posted January 8, 2008 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] Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-433547 Share on other sites More sharing options...
roopurt18 Posted January 8, 2008 Share Posted January 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84902-foreach_postx-as-key-value/#findComment-433696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.