1

我有一个 PHP 表单,它从数据库中提取数据,按行打印出来,然后允许用户检查他们需要哪些项目。保存时,只有最后一个选项被发送到 $_POST。这是我的代码:

global $dbc; 

$afQuery = "SELECT fid, facility_name, city, state, country, phone from facilities 
            WHERE uid = '123'
            AND fid != '456'
            GROUP BY fid;";
        $afResult = $dbc->query($afQuery,__FILE__,__LINE__);
        $i = 0;
        while ($affiliated = $dbc->fetch($afResult)) {      
            ++$i;
            echo '<div class="action" style="float:left;">';
                echo '<input type="checkbox" name="affiliated[afid]" id="afid" value="'.$affiliated->fid.'">';
            echo '</div>';
            echo '<div class="facility-name">';
                echo '<input type="hidden" id="facility-name '.$i.'" name="affiliated[facility_name]" value="'.$affiliated->facility_name.'" />';
                echo $affiliated->facility_name;
            echo '</div>';
            echo '<div class="facility-location">';
                echo '<input type="hidden" id="facility-location '.$i.'" name="affiliated[facility_location]" value="'.$affiliates->city. ', ' . $affiliated->state . ' ' . $affiliated->country.'" />';
                echo $affiliated->city. ', ' . $affiliated->state . ' ' . $affiliated->country;
            echo '</div>';
            echo '<div class="facility-phone">';
                echo '<input type="hidden" id="facility-phone '.$i.'" name="affiliated[facility_phone]" value="'.$affiliated->phone.'" />';
                echo $affiliated->phone;
            echo '</div>';
            echo '<div class="clear"></div>';
            echo '<input type="hidden" id="update" name="update" value="' . $affiliated->fid . '" />';
        }

$dbc->fetch相当于mysql_fetch_assoc

我正在尝试将这些值保存到$affiliated数组中,但它应该为每个选定的项目提供子数组,因此我可以将数据库中的每个值存储在单独的行中。我在哪里想念它?我在while循环中尝试了一个foreach,但我没有成功。

谢谢你的帮助!

* 更新 *

我遵循@Pinetree 的建议并添加$i如下:

<input type="checkbox" name="affiliated[afid]['.$i.']" id="afid" value="'.$affiliated->fid.'">

但是,现在我收到以下输出:

Array
(
    [afid] => Array
        (
            [1] => 5289
            [2] => 5290
            [3] => 5291
            [4] => 5292
        )

    [facility_name] => Array
        (
            [1] => Test Company
            [2] => Test Company 2
            [3] => Test Company 3
            [4] => Test Company 4
        )

    [facility_location] => Array
        (
            [1] => Address, State US
            [2] => Address, State US
            [3] => Address, State US
            [4] => Address, State US
        )

    [facility_phone] => Array
        (
            [1] => 555-555-5555
            [2] => 555-555-5555
            [3] => 555-555-5555
            [4] => 555-555-555
        )

)

我需要这个看起来像这样,所以我可以将每个完整的行插入到数据库中的表中:

Array
    (
        [0] => Array
            (
                [afid] => 5289
                [facility_name] => Test Company
                [facility_location] => Address, ST USA
                [facility_phone] => 555-555-5555
            )

    [1] => Array
        (
            [afid] => 5290
            [facility_name] => Test Company 1
            [facility_location] => Address, ST USA
            [facility_phone] => 555-555-5555
        )
)

建议?

4

2 回答 2

3

如果我理解正确,您将回显多行输入。如果是这种情况,您对每一行的输入使用相同的名称。当帖子发生时,它只会取最后一个值。您可以为每个输入名称添加另一个“[]”:

'<input name="affiliated[afid][]"/>'

甚至将 $i 放入其中:

'<input name="affiliated[afid]['.$i.']"/>'

在帖子中,您将拥有:

affiliated
    afid
        1: val
        2: val
        3: val
        ...
    facility_name
        1: val
        2: val
        3: val
        ...
    ...

整数对应于原始形式的行。所以 afid[1] 和 facility_name[1] 来自表格(和表格)中的同一行

编辑:要让数组恢复到它们在表单中的格式:

$affiliated = array();
foreach($_POST['affiliated']['afid'] as $k => $v) {
    $affiliated[] = array(
        'afid' => $v,
        'facility_name' => $_POST['affiliated']['facility_name'][$k] // same key here
         // other fields
    );
}

重要编辑

由于您有复选框,因此请务必在输入名称中显式设置索引:

'<input name="affiliated[afid]['.$i.']"/>'

如果未选中复选框,则不会发布复选框,并且文本字段和类似字段会发布,因此您可能会遇到一种情况,例如,第二个 POSTED 复选框(如果从 0 开始,则索引 1)将映射到 $_POST 中的第二行,但复选框实际上位于表单本身的第三或第四行,并且之前的复选框未发布

于 2012-08-14T21:34:11.870 回答
1

$affiliated不是一个数组,它是一个对象,并且您每次通过循环都(重新)设置它:

while ($affiliated = $dbc->fetch($afResult)){...}

此外,input通常 DOM 元素 ID 不能包含您正在生成的空格:

echo '<input type="hidden" id="facility-name '.$i.'"
于 2012-08-14T21:27:09.973 回答