如何在UI页面的选择框中显示数据库值

时间:2015-05-06 10:42:47

标签: php html

我想使用php ..在选择框中显示我的数据库值。

我的代码在这里:

SQL查询

$ home = mysql_query(“SELECT * FROM LOCATION WHERE LOCATION_ID ='$ updatehomeid'”)

或死(mysql_error());

$homeresult = mysql_fetch_array($home); 

$state = $homeresult['STATE'];

HTML

<select name="state" id="countySel2" class="choose" size="1"><option value="<?php echo $state; ?>" selected="selected">Select State</option></select>

如何在此选择框中显示db值。请任何人帮忙

感谢您提前

5 个答案:

答案 0 :(得分:0)

要在选择菜单上显示字符串,您应该使用:

<select name="state" id="countySel2" class="choose" size="1"><option value="<?php echo $state; ?>" selected="selected"><?php echo $state; ?></option></select>

该值未显示在UI中,它是服务器获取的内容

答案 1 :(得分:0)

您需要while循环,并且需要在选项中回显<?php echo $state; ?>

 <select name="state" id="countySel2" class="choose" size="1">    
 <option value="Select">Select State</option>    
 <?php  while ($homeresult = mysql_fetch_array($home)) { ?>
        <option value="<?php echo $homeresult['STATE']; ?>"><?php echo $homeresult['STATE']; ?></option>
 <?php } ?>
 </select>

答案 2 :(得分:0)

这可以帮助你...

<?php
     $qry = "select * from country";
?>
<select>
    <?php
        while($result = mysql_fetch_array($qry)){
    ?>
        <option value="<?php echo $result['state']; ?>"><?php echo $result['state']; ?></option>
    <?php
        }
    ?>
</select>

答案 3 :(得分:0)

PHP

<?php $home =mysql_query ("SELECT * FROM LOCATION WHERE LOCATION_ID = '$updatehomeid'" ) or die (mysql_error());if($home){while{$homeresult=mysql_fetch_array($home){$state = $homeresult['STATE'];}}?>

HTML

<select><option value="<?php echo $state;?>"><?php echo $state;?></option></select>

答案 4 :(得分:0)

希望这会对你有所帮助。

<?php 

$homeid = $_POST['home_id'] or $_GET['home_id'];
if($homeid == '') {
    $homeid = '3' // define default location id from database to prevent undefined value for fresh user 
}

$home = mysql_query ("SELECT * FROM LOCATION WHERE LOCATION_ID = '$homeid'" ) or die (mysql_error());

if($home){while{$homeresult=mysql_fetch_array($home){$state = $homeresult['STATE'];}}

?>

<select><option value="<?php echo $state;?>"><?php echo $state;?></option></select>