我在MySQL db中有两个表,即vendor
和menu
。
vendor table
vendor_id(Primary) vendor_name
menu table
menu_id(Primary) menu_details cost vendor_id(Foreign)
我需要创建一个组合框,动态地从MySQL数据库中获取vendor_name
个实例。在从组合框中选择vendor_name
时,应从MySQL数据库中显示菜单表(HTML和表应处于可编辑模式)。我是MySQL和PHP的新手。所以请帮我处理这个标准。
以下是我正在处理的代码:
<html><body>
<select name="vendor" id="vendor">
<option value="Choose">Choose</option>
<?php $conn = mysql_connect('localhost','root','');
if (!$con) { die('Could not connect: ' . Mysql_error()); }
mysql_select_db('project',$conn);
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
while($fetch= mysql_fetch_array($query)){
echo "<option>".$row[1]."</option>";
}?>
</select>
</body></html>
答案 0 :(得分:0)
如果我理解你,那么这应该是你所追求的事情。它显示来自vendor
表的组合框,如果更改选择,它将重新加载页面并显示与menu
表中的vendor_id相对应的菜单。
<html>
<body>
<?php
// connecting to the server
$conn = mysql_connect('localhost','root','') or die('Could not connect: ' . Mysql_error());
// selecting the database
mysql_select_db('project',$conn) or die("db error: ".mysql_error());
// is menu_id set?
if (isset($_GET['menu']) && intval($_GET['menu'])>0) {
// if yes, then query the menu items
$query = "select menu_id, menu_details, cost, vendor_id from menu where vendor_id=".intval($_GET['menu']);
$result = mysql_query($query, $conn) or die("SQL error: ".mysql_error());
while ($row = mysql_fetch_assoc($result)) {
// print the menu items
print_r($row);
}
}
// select the vendor combobox
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
// If the vendor selection has changed then reload the page
?>
<select name="vendor" id="vendor" onchange="document.location='index.php?menu='+this.options[this.selectedIndex].value;">
<option value="Choose">Choose</option>
<?php
// loop through the results
while($fetch= mysql_fetch_assoc($query)){
echo "<option value=\"".$row['vendor_id']."\">".$row['vendor_name']."</option>";
}
?>
</select>
</body>
</html>
假设该文件名为index.php
。 (查看select onchange=""
文件名。如果您有不同的文件名,则需要更改它。