Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2002
    Posts
    32

    Unanswered: foreach($_POST['x'] as $Key => $value) {

    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.

    My table structure looks like this

    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.

    OpenTable();
    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[{$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 "<input type='hidden' name='Session_ID' value='$Session_ID'>\n";
    echo "</form>\n";
    echo "</table>\n";
    CloseTable();
    GRAT_Grade[{$Group_ID}] is an array with Group_ID

    I have used this to see what my array looks like.

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

    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
    )
    It is correct.

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

    I just can't seem to extract what I need and update the table.

    $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());
    }
    I hard coded a Session_ID = 43 to see if the values was not passed but so far no good.

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    In the last part of what you posted you specified the update value to be ...SET GRAT_Grade = '$GRAT_Grade' WH...
    So in order to help you, i'll ask you what $GRAT_Grade equals?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •