我得到一个未定义的索引:每个超过一千的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
答案 0 :(得分: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;
}
如果你这么做了,你可能想要编写一个实用程序函数,它接受keys
和values
以及零个或多个选定键/值的数组,并返回组合的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);
答案 1 :(得分:0)
类似
$options.="<option value = \"$id\"";
if($bamid == $id) $options.= " selected";
$options.=">".$name.'</option>';