在下拉选项中组合两个mysql表结果并检索选中

时间:2013-07-16 05:52:13

标签: php mysql

您好我有一个下拉列表,它将从表(data_builder)中检索值,并希望组合另一个表(data_suburb)的现有郊区。因此,当我点击下拉列表时,它将显示所选的郊区和data_suburb的所有郊区

示例I具有:

TABLE(data_builder)

<b>id | builder_name | builder_suburb | builder_state | builder_postcode</b><br>

   1  | Biz Shop     | Richmond       | VIC           | 3121<br>
   2  | Tony Sykes   | Melbourne      | VIC           | 3000<br>
   3  | HDI Group    | Abbotsford     | VIC           | 3607<br>

等...

TABLE(data_suburb)

<b>id | suburb       | suburb_state  | suburb_postcode</b><br>

   1  | Abbotsford   | VIC           | 3067<br>
   2  | Aberfeldie   | VIC           | 3040<br>
   3  | Aberfeldy    | VIC           | 3825<br>

等...

这是我尝试过的代码

include $_SERVER['DOCUMENT_ROOT']."/includes/conn.php";
$id =$_REQUEST['id'];
$result = mysql_query("SELECT * FROM data_builder WHERE id  = '$id'");
$retrieve = mysql_fetch_array($result);
$BuildSuburb=$retrieve['builder_suburb'] ;

echo "<select name='buildersuburb'>";
      $query="SELECT suburb FROM data_suburbs ORDER BY suburb ASC";
      $result = mysql_query($query);


            if(!$result){

                die ("Could not query the database: <br />" . mysql_error());

            }

while ($data=mysql_fetch_array($result)){
             echo '<option value="' . $data[suburb];
             if($data[suburb]=$BuildSuburb): echo $data[suburb] . '"' .     'selected="selected"';
             else: echo $data[suburb] . '"';
             endif;
             echo '" >' . $data[suburb] . '</option>'. "\n";

}




echo "</select>";

1 个答案:

答案 0 :(得分:0)

在循环$ data = mysql_fetch_array($ result)中将以下行更改为

$ data = mysql_fetch_assoc($ result)因为您正在使用$ data ['suburb']

同样在您的$查询中,不需要ASC