从数据库下拉菜单没有结果

时间:2013-06-01 08:32:36

标签: php html mysql

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

<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>

6 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

你的意思是

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

$ options 也应采用以下格式:

$options .= '<option value="'.$name.'">'.$name.'</option>';

答案 2 :(得分: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>

答案 3 :(得分: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-->

答案 4 :(得分: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>

答案 5 :(得分:-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 ?>