我知道我编码错误,但似乎找不到纠正它的方法。目的是显示一个下拉列表,其中填充了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>';
}
?>
答案 0 :(得分:6)
如果你的目标是让所有选项都有一个下拉列表,那么你需要为while的外部的打开和关闭标签放置回声并离开选项标签 in while:
// 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>';
答案 1 :(得分:3)
试试这个:
echo '<select name="customer">';
echo '<option value="">Select delivery address</option>';
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 '<option value="address">'.$address.'</option>';
}
echo '</select>';
答案 2 :(得分:1)
while
循环每次都会生成一个select
元素。
你应该把select
放在while
echo "<select>";
while(/* while statement */){
echo "<option></option>";
}
echo "</select>";
答案 3 :(得分:1)
将您的时间改为此
echo '<select name="customer">';
echo '<option value="">Select delivery address</option>';
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 '<option value="address">'.$address.'</option>'
}
echo '</select>';
答案 4 :(得分:1)
尝试这样的事情:
echo '<select name="customer">';
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 '<option value="">Select delivery address</option>'.'<option value="address">'.$address.'</option>';
}
echo '</select>';
你们每个人(表中的一行)都有所不同,希望这有助于:)。