0

我试图从数据库中获取值并将它们显示在下拉菜单中,但我在下拉菜单中什么都没有,这是代码请任何人?

<select name="car" value="Select" size="1">

<?php
$sql = "SELECT fullname FROM users"; 

    $result = mysql_query($sql) or die (mysql_error()); 

    while ($row = mysql_fetch_array($result))

    {
            $name=$row['fullname']; 
            $options.="<OPTION VALUE=\"$name\">";
    }

?>

<option>
<? echo $options ?>
</option>
</select>
4

6 回答 6

1

您的代码中有几个错误。试试这个:

<select name="car" value="Select" size="1">

<?php
$sql = "SELECT fullname FROM users"; 

    $result = mysql_query($sql) or die (mysql_error()); 

    while ($row = mysql_fetch_array($result))
    {
            //securing from XSS
            $name= htmlentities($row['fullname']); 

            //you had no closing tag, no name
            $options.="<OPTION VALUE=\"$name\">$name</option>";
    }
    //need semicolon, no need for tags
    echo $options;
?>

</select>
于 2013-06-01T08:42:25.010 回答
0

你的意思是

<select name="car" value="Select" size="1">
<? echo $options; ?>
</select>

$options也应采用以下形式:

$options .= '<option value="'.$name.'">'.$name.'</option>';
于 2013-06-01T08:35:55.253 回答
0

将您的代码更改为此..

<select name='cars'> <?php $sql = "SELECT fullname FROM users"; 

$result = mysql_query($sql) or die (mysql_error()); 

while ($row = mysql_fetch_array($result))

{
        echo "<OPTION VALUE='".$row['fullname']."'>".$row['fullname']."</OPTION>";
}?></select>
于 2013-06-01T08:50:07.780 回答
0
in your code Semicolon(;) is missing in the end of $options:

<option>
<? echo $options ?>
</option>

also remove `value="Select"` with select tag

try this:

<select name="car" size="1">

<?php
    $sql = "SELECT fullname FROM users";     
    $result = mysql_query($sql) or die (mysql_error()); 

    while ($row = mysql_fetch_array($result))

    {
            $name=$row['fullname']; 
           echo "<OPTION VALUE='$name'>$options</option>";
    }

?>
</select>
于 2013-06-01T08:37:23.367 回答
0

你错过了选项完整的语法 $options.="<OPTION VALUE=\"$name\">"; 应该是 $options.="<OPTION VALUE=\"$name\">$name</OPTION>";

编辑

<option><!--< not required as you already used in $options-->
<? echo $options ?>
</option><!--< not required as you already used in $options-->
于 2013-06-01T08:38:36.650 回答
-1

如果您在表格中有条目,只需从您的代码中删除额外的选项

<select name="car" value="Select" size="1">

<?php
$sql = "SELECT fullname FROM users"; 

    $result = mysql_query($sql) or die (mysql_error()); 

    while ($row = mysql_fetch_array($result))

    {
            $name=$row['fullname']; 
            $options.="<OPTION VALUE=\"$name\">";
    }

?>
<? echo $options ?>
</option>
</select>

<option>之前 删除<? echo $options ?>

于 2013-06-01T08:37:06.423 回答