0

可能重复:
如何使 <option selected="selected"> 由 MySql 和 PHP 设置?

我在我的页面中进行了选择。此 Select 从数据库中提取选项。选择是:

<select name="name" id="name">
<?php
$pelatisquery="SELECT onoma_pelati FROM pelates_onoma";
$pelatisresult=mysql_query($pelatisquery) 
or die ("Query to get data from firsttable failed: ".mysql_error());
while ($pelatisorow=mysql_fetch_array($pelatisresult)) {
$onoma_pelati=$pelatisorow[onoma_pelati];
echo "<option>
$onoma_pelati
</option>";
}
?>
</select>

此选择在我的主页中。因此,在“编辑”页面中,我想使用从数据库中选择的一个选项来检索此 Select。所以我的编辑页面是:

include_once "../../db_connection.php";
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
$result = mysql_query("SELECT id,pelatis FROM poliseis WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$pelatis = $row['pelatis'];
}
}

因此,如果 Select 有选项,我将使用以下代码:

<?php if($pelatis== "James") { echo 'selected = "selected"'; } ?>

但是现在怎么弄<option selected="selected">?预先感谢您的回答

4

1 回答 1

1

谷歌是你的朋友。

<option value="" selected="selected"></option>

<option value="" selected></option> //works too but less cross-browser friendly
于 2012-12-14T00:14:49.260 回答