-1

尝试从数据库中更新和删除记录,但我的 $key 总是返回零。这是我的代码。如果我需要澄清任何事情,请告诉我。谢谢你。

请注意,我没有包括下面“while”的选择语句。

        // Fetch rows:      
    while ($Row = mssql_fetch_assoc($result5)) {

        $id = $Row['id'];
        $notes = $Row['RejectedNotes'];
        $first = $Row['FIRST'];
        $last = $Row['LAST'];
        $ssn = $Row['SSN'];
        $psn = $Row['PSN'];
        $dob = $Row['DOB'];
        $distdocket = $Row['DistDocket'];
        $releasedate = $Row['ReleaseDate'];
        $releasetype = $Row['ReleaseType'];

        $columnData4 =  "<table class='footerTable'><tr><td class='footerFirst'>"
        . $first .              "</td><td class='footerLast'>"
        . $last .               "</td><td class='footerSSN'>"
        . $ssn .                "</td><td><input type='text' class='footerNMCD' name = 'psn[]' value = '"
        . $psn .                "'></td><td class='footerDOB'>"
        . $dob .                "</td><td class='footerCause'>"
        . $distdocket .         "</td><td><input type='text' class='footerRelease' name = 'releasedate[]' value = '"
        . $releasedate .        "'></td><td>"
        .                       "<select name='relOptions[]' id='releaseOptions' class='footerEvent'>"
        .                       "<option value='$releasetype'>$releasetype</option>"
        .                       "<option value='Discharge'>Discharge</option>"
        .                       "<option value='In-House Parole'>In-House Parole</option>"
        .                       "</select></td>"
        .                       "<td class='reason'>"
        .                       "<button class='reason' onclick='openNotesBox(\"$notes\");'>View</button></td><td>"
        .                       "<input type='checkbox' name='checkbox[]' value=\"$id\" />$id</td></tr>";
        print $columnData4; 
    }
    print "</form></table>";


                // I know this is strange code, but it's what I had to do for Chrome.
    if(isset($_REQUEST['resubmit']) || isset($_REQUEST['remove']))
    {                   
        $checkbox=$_POST['checkbox'];               
        $nmcd=$_POST['psn'];
        $release=$_POST['releasedate'];
        $relOptions=$_POST['relOptions'];                           
        //HERE IS MY ISSUE!!!  $key is always "0".  
        foreach ($checkbox as $key => $value)
        {
            $nmcd = $nmcd[$key];
            $release = $release[$key];
            $releasedate = substr($release, 0, 2) . "-" . substr($release, 2, 2) . "-" . substr($release, 4, 4);
            $relOptions = $relOptions[$key];


            if(isset($_REQUEST['remove']))
            {           
                $del = "DELETE FROM submitted WHERE id='$value'";
                $result = mssql_query($del);
            }
            else {  
                if(isset($_REQUEST['resubmit']))
                {
                    $resub = "UPDATE submitted 
                             SET PSN = '$nmcd', ReleaseDate='$releasedate', ReleaseType='$relOptions', Rejected='0'
                             where id='$value'";
                    $result = mssql_query($resub);
                }           
            }       
        }
    }
4

1 回答 1

1

您应该使用$value复选框,而不是$key. 您的代码如下:

<input type='checkbox' name='checkbox[]' value=\"$id\" />$id</td>

在其中您没有为数组指定索引值checkbox[],因此所有值都将按照设置的顺序添加。正如@ilmari-karonen 在引用PHP - Checkbox group的注释中指出的那样,如果您希望在表单数据中接收它,则必须在 HTML 中指定一个有意义的值。IE:

<input type='checkbox' name='checkbox[$some_index]' value=\"$id\" />$id</td>
于 2013-01-03T21:04:21.383 回答