1

我得到一个未定义的索引:对于每个超过一千的 ID。我已尽一切努力使错误消失,并希望有人能够提供帮助。请提前致谢。

$bamid 是从页面调用的,它可以工作。错误来自这个语句“$selected[$id]”,如果删除它就没有错误。添加双引号或单引号不起作用。

<?
$sql="SELECT * users ORDER BY name ASC";
$result=mysql_query($sql);
$options = "";
while ($row=mysql_fetch_array($result))
{
$id = $row['id'];
$name = $row['name'];
$selected = array($bamid => "selected");
$options.="<option value = \"$id\" $selected[$id]>".$name.'</option>';
}
?>
<select name = "bamid">
<option>-------</option>
<? echo $options?>
</SELECT>

错误日志的示例输出:

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 572 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 833 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 698 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 666 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 546 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 688 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 834 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 312 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 650 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 1109 in xxxxxxxxxxxxxxx on line 173

[Sun Jun 30 01:46:55 2013] [warn] [client xx.xx.xx.xx] mod_fcgid: stderr: PHP Notice:  Undefined index: 430 in xxxxxxxxxxxxxxx on line 173
4

2 回答 2

0

$id如果当前等于,您的代码会尝试添加“选定” $bamid,但在所有其他情况下它会做什么?它不知道,所以它会发出警告。

它仍然有效,因为未定义的字符串在 PHP 中呈现为空字符串,但最好明确说明这一点。

<?
// BTW, this opening tag would be better as <?php

// #include standard recommendation on moving to PDO instead of deprecated MySQL
// support

while ($row=mysql_fetch_array($result))
{
    $id = $row['id'];
    $name = $row['name'];
    $selected = array($bamid => "selected");
    $options.="<option value = \"$id\" $selected[$id]>".$name.'</option>';
}

你可以这样做:

$selected = ($id == $bamid) ? 'selected="selected"' : '';
$options.="<option value=\"$id\" $selected>$name</option>";

或者,也许以重复为代价来制作更具可读性的代码:

if ($id == $bamid) {
    $options.= <<<OPTION_SELECTED

<option value="{$id}" selected="selected">{$name}</option>

OPTION_SELECTED;
} else {
    $options.= <<<OPTION_NOT_SELECTED

<option value="{$id}">{$name}</option>

OPTION_NOT_SELECTED;
}

如果您经常这样做,您可能想要编写一个实用函数,该函数接受一个数组keysvalues零个或多个选定的键/值,并返回 HTML 以获取选项组合框列表。这会导致

$optarr = array();
while ($row=mysql_fetch_array($result))
{
    $id   = $row['id'];
    $name = $row['name'];

    $optarr[$id] = $name;
}
mysql_free_result($result);

$options = htmlOptions($optarr, $bamid);
于 2013-06-30T06:04:56.840 回答
0

就像是

$options.="<option value = \"$id\"";
if($bamid == $id) $options.= " selected";
$options.=">".$name.'</option>';
于 2013-06-30T06:05:43.390 回答