I am getting an Undefined index: for every ID which is over a thousand. I have tried everything to get the error to disappear and am hoping someone would be able to help. Please and Thanks in advance.
$bamid is what is called from the page and it works. The error comes from this statement "$selected[$id]" which there are no errors if removed. Adding double or single quotes does not work.
<?
$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>
Sample Output from the Error Log:
[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
Your code attempts to add a "selected" if the current $id
equals $bamid
, but what it does in all other cases? It doesn't know, so it throws a warning.
It still works, because undefined strings are rendered as empty strings in PHP, but it would be better to state this explicitly.
<?
// 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>';
}
You can do it like this:
$selected = ($id == $bamid) ? 'selected="selected"' : '';
$options.="<option value=\"$id\" $selected>$name</option>";
or, maybe making for more readable code at the expense of duplication:
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;
}
If you do this a lot, you might want to write a utility function that takes an array of keys
and values
and zero or more selected keys/values, and returns the HTML for a combo box list of options. This would lead to
$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);