1

未定義のインデックスを取得しています: 1,000 を超えるすべての 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;
}

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);
于 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 に答える