第一次选择标签后不包括选项

时间:2017-04-03 09:37:23

标签: php html

相对较新的php,我想获取一个名为make的列,我正在使用循环和mysqli_fetch_row从列中获取每一行,然后我将数据插入到select标签中。我面临的问题是它第一次将数据插入到选项标签中,之后它只是将其从列表中回显。 这是代码,

$SQLstring = "select make from inventory";
$queryResult = @mysqli_query($DBConnect, $SQLstring)
Or die ("<p>Unable to query the $TableName table.</p>"."<p>Error code ". mysqli_errno($DBConnect). ": ".mysqli_error($DBConnect)). "</p>";
//$row= mysqli_fetch_all($queryResult);
$i=0;
echo"<label for='make'>Please select a make:</label>
<select>
    <option name='make' value='All'>All</option>";
    while($row= mysqli_fetch_row($queryResult)){
    echo"<option value='$row[$i]'>$row[$i]</option>
</select>";
    }

这里有什么d0?

5 个答案:

答案 0 :(得分:0)

试试这个:

<select>
while($row= mysqli_fetch_row($queryResult)){
   echo '<option value="'.$row[$i].'">'.$row[$i].'</option>';
}
</select>

只在循环中放置option标记。

答案 1 :(得分:0)

将选择放在while循环之外

    $SQLstring = "select make from inventory";
    $queryResult = @mysqli_query($DBConnect, $SQLstring)
    Or die ("<p>Unable to query the $TableName table.</p>"."<p>Error code ". mysqli_errno($DBConnect). ": ".mysqli_error($DBConnect)). "</p>";
    //$row= mysqli_fetch_all($queryResult);
    $i=0;
    echo"<label for='make'>Please select a make:</label>
    <select>
        <option name='make' value='All'>All</option>";
        while($row= mysqli_fetch_row($queryResult)){
        echo"<option value='$row[$i]'>$row[$i]</option>";
        }
    echo "</select>";

答案 2 :(得分:0)

正如评论中所述,可能的问题是您</select>的放置。

试试这个:

echo "<label for='make'>Please select a make:</label><select>";
echo "<option name='make' value='All'>All</option>";
while($row= mysqli_fetch_row($queryResult))
    echo "<option value='$row[$i]'>$row[$i]</option>";
echo "</select>";

像这样,它只会输出&#39; select&#39;一旦在选项之后。

作为一个额外的提示,我建议您查看调试工具,例如&#39; Firebug&#39;这将帮助您通过检查您所看到的页面背后的HTML来帮助您找到类似的问题。

答案 3 :(得分:0)

试试这个:

<select>
<option name='make' value='All'>All</option>";
while($row= mysqli_fetch_row($queryResult)){
  echo "<option value='$row[$i]'>$row[$i]</option>";
}
</select>

由于你对php&#34;相对较新,所以这里有一个很好的阅读为什么你不应该混合php和HTML代码,或者换句话说,为什么你应该将应用程序逻辑与表示http://thisinterestsme.com/mixing-php-html/

分开

希望这有帮助!

答案 4 :(得分:0)

只需将name属性添加到select标签并从选项标签中删除。

$SQLstring = "select make from inventory";
$queryResult = @mysqli_query($DBConnect, $SQLstring)
Or die ("<p>Unable to query the $TableName table.</p>"."<p>Error code ". mysqli_errno($DBConnect). ": ".mysqli_error($DBConnect)). "</p>";
$i=0;
echo"<label for='make'>Please select a make:</label>
<select name='make'>
<option value='All'>All</option>";
while($row= mysqli_fetch_row($queryResult)){
echo"<option value='$row[$i]'>$row[$i]</option>
</select>";
}