简体   繁体   English

从数据库创建选择列表

[英]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". 我正在使数据库表中的选择列表一切正常,只有选项值中有问题,如果该值包含“ New York”之类的两个单词,则它仅返回“ 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 如果您查看代码为选择框创建的html,则类似于

<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标记相同的值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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