我想查看一些我通过html和php代码添加到数据库中的食物 像食物a,食物b,食物c,食物d。
我希望在html的另一页下拉列表中查看这些食物类别作为更新食品类别。
<?php
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'rfid'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
?>
<div class="sign_up">
<div class="formtitle" align="center">View Food Items </div>
<div class="top_section">
<div class="section">
<br>
<div class="input-sign details2">
<form name="form1" action="" method="post">
View Foods:
<?php
if ($result = @$dbconn->query("SELECT * FROM `food`"))
{
//`food` represents the table name.
echo "<select>";
while ($row = $result->fetch_assoc())
{
echo "<option value=\"".$row["food"]."\">".$row["food"]."</option>";
/*"id" and "foodname" represents the table column name.*/
}
echo "</select>";
}
?>
<select>
<option><?php echo $row["food"];?></option>
</select>
</form>
</div>
</div>
</div>
<div class="clear"> </div>
</div>
&#13;
答案 0 :(得分:2)
这可能与您的报价有关,请尝试echo "<option value=\"".$row['food']."\">".$row["food"]."</option>";
此外,我发现this类似问题可以帮助您解决问题。致作者@SpaceBeers
答案 1 :(得分:0)
所以我假设您知道如何连接数据库并使用它。
以下是您想要的示例。
if ($result = $dbconn->query("SELECT * FROM `food`")) {
//`food` represents the table name.
echo "<select>";
while ($row = $result->fetch_assoc()) {
echo "<option value=\"".row["id"]."\">".row["foodname"]."</option>";
/*"id" and "foodname" represents the table column name.*/
}
echo "</select>";
}