2012-11-17 21 views
0

我在MySQL數據庫中有兩個表,即vendormenu基於使用MySQL和PHP的組合框中的值的動態顯示錶

vendor table 
vendor_id(Primary) vendor_name 

menu table 
menu_id(Primary)  menu_details cost  vendor_id(Foreign) 

我需要建立一個獲取vendor_name情況下從MySQL數據庫動態的組合框。在從組合框中選擇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

你需要向我們展示你已經試過的(stackoverflow人),並努力使你的代碼工作。代碼? – samayo

+1

這裏是一些使用MySQL與PHP的文檔:http://www.php.net/manual/en/mysqli.query.php –

+0

動態顯示組合框從MySQL數據庫獲取值,我已經寫了下面的code.But IM沒能獲得在組合框中的值。 –

回答

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。 (查看onchange=""的文件名,如果你有不同的文件名,你需要改變它。

相關問題