我正在尝试从数据库中创建一个下拉菜单。我现在什么也没回来。对于菜单,我需要选择一个对象,然后对其运行SQL查询。此查询将填充表格并将是动态的。 这是代码,请帮忙。
<html>
<?php
include('header.php');
?>
<h1>Chemicals Search</h1>
<br/ >
<br/ >
</head>
<h1>
<center>Chemical Search</center>
</h1>
<form action="chemicals.php" method="post">
<label>Search By Product:</label>
<?php
//making the chemical array
$con = mysqli_connect("...");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$p = mysqli_query($con, "SELECT product_name FROM product");
$products = mysqli_fetch_array($p);
echo '<select name="product">';
foreach ($product as $key => $value) {
echo "<option value=\"$key\"> $value</option>\n";
}
echo '</select>';
echo "<br>";
?>
</form>
答案 0 :(得分:1)
您的查询仅选择产品名称对其进行一些更改
$p = mysqli_query($con,"SELECT product_id,product_name FROM product");
echo'<select name="product">';
if(mysqli_num_rows($p) > 0)
{
while($products =mysqli_fetch_array($p))
{
echo"<option value='".$products['product_id']."'>".$products['product_name']."</option>\n";
}
}
echo '</select>';
echo"<br>";
我相信这会有效,每次都适合我