简体   繁体   中英

Creating select list from database

I am making select list from database table everything works fine only issue is in option value if the value consist of two words like "New York" it only returns "New". Here is the code

<?  $country=intval($_GET['country']);

include('connect-db.php');

$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);

?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['CityName']?>><?=$row['CityName']?></option>
<? } ?>
</select>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>

“”。

您必须确定“”中的值,HTML代码认为York部分只是option标记的另一个属性,因此:

<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>

You haven't included the "" while giving the values. if you look at the html created for the select box by your code is something like

<option value="new" york>new york</option>

correct solution is given below, just included the quotation while giving value of option tag.

<?  $country=intval($_GET['country']);

include('connect-db.php');

$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);

?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
<? } ?>
</select>

只需使用“”作为与编写HTML标记相同的值即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM