我尝试从数据库创建产品类型的下拉列表,我使用How to populate HTML dropdown list with values from database中的代码。
<select name="product_type">
<?php
include('include/conn.php');
$sql = mysql_query("SELECT product_type FROM produk2");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"product_type1\">" . $row['product_type'] . "</option>";
}
?>
</select>
我将在我的搜索引擎中添加此内容,但该代码的下拉菜单会使其读取product_type中的所有数据。是否可以在没有相同product_type两次的情况下创建下拉列表?
答案 0 :(得分:1)
你至少应该使用mysqli连接:
conn.php =
$con=mysqli_connect("server","username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
然后是您的查询文件:
<select name="product_type">
<?php
include('include/conn.php');
$sql = mysqli_query($con,"SELECT product_type FROM produk2 GROUP BY product_type")
while ($row = mysqli_fetch_array($sql)){
echo "<option value=\"product_type1\">" . $row['product_type'] . "</option>";
}
?>
</select>