HTML从数据库中选择值

时间:2015-01-09 21:48:51

标签: php mysqli

有人能告诉我为什么这段代码没有将任何内容返回到选择框中吗?

<select name="student" id="student"> 
<option value="">Select Student</option>
<?php
$sql=mysqli_query("SELECT name FROM users WHERE flag = 1");
while($row=mysqli_fetch_assoc($sql)) {
    echo "<option value='studname' name='studname'> $row[name] </option>";
}
?>
</select>

2 个答案:

答案 0 :(得分:0)

你应该在php中连接字符串,例如:

echo   "<option value='studname' name='studname'>" . $row['name'] .  "</option>";

答案 1 :(得分:-1)

已编辑执行此操作:

<select name="student" id="student"> 
<option value="">Select Student</option>

<?php
include_once("dbConnect.php");

$sql=mysqli_query($dbCon,"SELECT name FROM users WHERE flag = 1");
while($row=mysqli_fetch_assoc($sql)) {
    echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select>