* Andrew
> Can anyone tell me why this mysql query and 'selected' isn't
> working?  It does keep a fixed id selected but not the one
> that was selected and aslo doesn't return any results?

Try to keep the html part of the problem separate from the mysql part of the
problem... :)

> echo "<select name=\"CityID\" size=\"1\" class='menuForm'>";
>
> $result=mysql_query("SELECT City, CityID FROM city ORDER BY City");
> while ($row = mysql_fetch_array($result))
>     {
>         $city_id=$row['CityID'];
>               $city=$row['City'];
>
> echo "<option value=\"$city_id\" selected> $city </option>";
> }
> echo "</select>";

If the query "SELECT City, CityID FROM city ORDER BY City" gives a result
when run in the mysql client, your problem is only with html.

Your code above writes "selected" on all the options, it should be only on
the one option actually selected. A browser will normally select the first,
when multiple options are marked as selected. You need to check with an
if-statement if the current option is the selected option:

while ($row = mysql_fetch_array($result)) {
  $city_id=$row['CityID'];
  $city=$row['City'];
  echo "<option value=\"$city_id\"";
  if ($CityID == $city_id)
    {echo " selected";}
  echo "> $city </option>";
}

"$CityID" contains the id of the selected column, because "CityID" is the
name of the select element.

HTH,

--
Roger


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to