如何设置默认选择的选项

时间:2014-07-13 11:06:17

标签: php sql

我有一个下拉列表,它从db

获取其值
 $sql = "SELECT *";
                 $sql .= "FROM `inventory`";
                  $query = mysql_query($sql);
            while ($fetch = mysql_fetch_array($query)){
                echo '<option value="'.$fetch['In_id'].'">'.$fetch['In_name'];}

现在,如果['In_id']发送,请为所选选项设置值['In_name']。

if (isset($_GET['in_id'])){
$in_id = $_GET['in_id'];
            $sql_in = "SELECT *";
             $sql_in .= "FROM `inventory` WHERE In_id='$in_id'";
              $query_in = mysql_query($sql_in);
        $fetch_in = mysql_fetch_array($query_in);
        }

1 个答案:

答案 0 :(得分:1)

尝试更改while循环,如下所示:

 while ($fetch = mysql_fetch_array($query)){
  $in_id = $_GET['in_id'];
  $sel = '';
  if($in_id == $fetch['In_id']){
        $sel = 'selected="selected"';
  }
  echo '<option value="'.$fetch['In_id'].'" '.$sel.'>'.$fetch['In_name'].';}