<?php
require ('mysql_connect.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
<html>
<head> <title> Training booking </title> </head >
<body>
<p > <b> Student Training Booking Form </b> </p >
<form action = "training.php " method = "get " >
<label> Name :
<input name=" name " placeholder=" Name " type=" "><br />
Email :
<input name=" email " placeholder=" example@example.com "
type=" "><br />
</label>
</form >
<p > Please Select a Training session </p >
<select name="top">
<option value="0">--TOPICS--</option>
<?php
$query = ("SELECT topic_name FROM topic");
while ($row = mysqli_fetch_array($query))
?>
{
<option> <?php echo $query ['topic_name'] ?> </option>
}
</select>
嗨,我试图用MySQL填充一个简单的下拉列表,并且下拉列表没有填充它只是保持空白。有人可以帮帮我吗。(我知道代码到处都是。)
更新
代码现在看起来像这样,它仍然无效。
<p > Please Select a Training session </p >
<?php
$sql = "SELECT topic_name FROM topic";
$query = mysqli_query($sql);
?>
<select name="topic">
<option value="0">--TOPICS--</option>
<?php
while ($row = mysqli_fetch_array($query)) {
?><option><?php echo $row ['topic_name'] ?></option><?php
}
?>
</select>
任何人都知道哪里出错了?
答案 0 :(得分:1)
你正在看&#34; S&#34;在您的下拉列表中,因为您将查询字符串视为一个数组并显示第一个字母:
echo $query['topic_name']
必须先执行查询才能获取结果行 比较mysqli_query和mysqli_fetch_array。
$query = "SELECT topic_name FROM topic";
$result = mysqli_query($mysqli,$query);
while ($row = mysqli_fetch_array($result)) {
?><option><?=$row['topic_name']?></option><?php
}
顺便说一句,我还建议使用prepared statements。