动态填充'<select>'并检索所选值</select>

时间:2013-05-14 10:06:13

标签: php html mysql

你好我用数据库中的数据填充了一个下拉列表,现在我想检索所选的值并回显它但它没有工作,它一直空着。

<select>
   <option value="0">--None Selected--</option>
<?php
$dbhost = 'localhost';
$dbuser = 'Phil';
$dbpass = '********';

// Fetching UserGroupID int TextBox
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

$sql = "SELECT * FROM   usergroups";
mysql_select_db('dopetunez');


$result = mysql_query($sql);

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//  then create $userid, $fullname, and $userstatus

while ($row = mysql_fetch_assoc($result)){  

?>

<option value="<?php echo $row["UserGroupID"] ?>"> <?php echo $row["UserGroup"]?>
</option>

<?php
    }
?>
</select>

2 个答案:

答案 0 :(得分:1)

乍一看.. 你应该在执行查询之前选择db ..所以替换

 $sql = "SELECT * FROM   usergroups";
 mysql_select_db('dopetunez');

  mysql_select_db('dopetunez');
  $sql = "SELECT * FROM   usergroups";

建议请使用mysqli_ PDO。不再维护mysql _

答案 1 :(得分:0)

您应首先选择数据库。然后只有您可以访问查询。请改变这样,

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('dopetunez');

$sql = "SELECT * FROM   usergroups";