php while循环输出为每个项目生成select

bol*_*llo 2 php

我知道我编码错误,但似乎找不到纠正它的方法.目标是显示一个下拉列表,其中填充了mysql数据库的结果.它目前显示每个地址的下拉列表.我知道为什么会这样,但似乎无法纠正它.回声是否应该超出while循环?或者是正确的位置,但在其他地方是错误的?如果有人能检查它并告诉我我的错误在哪里,我会很感激的.非常感谢.

<?php
    $customer = mysql_real_escape_string( $_GET["customer"] );         
    $con = mysql_connect("localhost","root","");
    $db = "sample";
      if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($db, $con);
        $query_rs_select_address2 = sprintf("SELECT * FROM company_com where idcode_com = '$customer'");
        $rs_select_address2 = mysql_query($query_rs_select_address2, $con) or die(mysql_error());
        $row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2);
        $totalRows_rs_select_address2 = mysql_num_rows($rs_select_address2);

        while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
            {  
        $address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
        echo '<select name="customer">'.'<option value="">Select delivery address</option>'.'<option value="address">'.$address.'</option>'.'</select>';

            }

?>
Run Code Online (Sandbox Code Playgroud)

cwa*_*ole 6

如果你的目标是拥有一个下拉将所有的选项,那么你需要把回声选择的打开和关闭标签之外的同时并保留选项标签的同时:

// also put first option on the outside as you *don't* want to repeat that.
echo '<select name="customer"><option value="">Select delivery address</option>';
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
{  
    // only place the code in here you want repeated for every value in the query
    /* 
        have you considered concatenating in the query?
        Basically:
        select concat( address1_com, ' ', address2_com, ' ', address3_com, ' '
               town_com, ' ', postcode_com ) as full_address From...

        Sometimes the greater number of records means slower execution.
        Of course, you should benchmark to be sure.
    */
    $address=  $row_rs_select_address2['address1_com']. " ". 
               $row_rs_select_address2['address2_com']. " ". 
               $row_rs_select_address2['address3_com']. " ". 
               $row_rs_select_address2['town_com']. " ". 
               $row_rs_select_address2['postcode_com'];
    // notice I swapped out $uid for address. You want to have each option 
    // reflect a different value so that the POST gives a uid to the server
    // You can probably get a uid from the primary key of the company_com 
    // table.
    echo '<option value="$uid">'.$address.'</option>';

}
echo '</select>';
Run Code Online (Sandbox Code Playgroud)