使用mySQL和PHP填充<select> </select>

时间:2012-11-14 19:26:46

标签: php mysql

我有以下下拉列表

<select name="SelP">
    <option value="0">Please Select...</option>
    <option value="35">LS1</option>
    <option value="35">LS2</option>
    <option value="35">LS3</option>
    <option value="35">LS4</option>
    <option value="35">Postcode not Listed</option>
</select>

我写的PHP就像这样工作

<?php
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="localhost";
$username="alloytes_achenry";
$password="**PASSWORD HERE**";
$dbname="alloytes_acquote";
$usertable="LocationTable";
$postcode="postcode";
mysql_connect($hostname,$username, $password) or die ("    <html>%MINIFYHTML4333ddb1f6ba50276851b9f9854a5c817%</html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = "SELECT * FROM $usertable WHERE minimumprice = 35";
$result = mysql_query($query);
if($result)
{
while($row = mysql_fetch_array($result))
{
$postcode = $row["$postcode"];
echo "$postcode";
}
}
?>

这很好用并返回邮政编码值。我现在需要编辑PHP以返回 $ postcode $ minimumprice ,然后填充表格,邮政编码作为文本,$ minimumprice作为值(值= “$ minimumprice”)。

任何建议从哪里开始将非常感谢

由于

亨利

测试代码1

<?php
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="localhost";
$username="alloytes_achenry";
$password="f(6@g0^O#2kT";
$dbname="alloytes_acquote";
$usertable="LocationTable";
$postcode="postcode";
$minimumprice="minimumprice";
mysql_connect($hostname,$username, $password) or die (" <html>%MINIFYHTML4333ddb1f6ba50276851b9f9854a5c817%</html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if($result)
{
while ($row = mysql_fetch_assoc($result)) {
$postcode = $row['postcode'];
$minimumprice = $row['minimumprice'];
echo <<<EOL
<option value="$minimumprice">$postcode</option>

EOL;
}
}
?>
</form>

只返回邮政编码,任何想法?

由于

3 个答案:

答案 0 :(得分:2)

这样的东西?

while ($row = mysql_fetch_assoc($result)) {
   $postcode = $row['postcode'];
   $minimumprice = $row['minimumprice'];
   echo <<<EOL
<option value="$minimumprice">$postcode</option>

EOL;
}

请注意使用heredoc

答案 1 :(得分:1)

$select = '<select name="SelP"> <option value="0">Please Select...</option>';
$result = mysql_query($query);
if($result){
    while($row = mysql_fetch_array($result)) {
        $postcode = $row["postcode"];
        $minimumprice = $row["minimumprice"];
        $select .= "<option value=\"$postcode\">$minimumprice</option>";
    }
}
$select .= '</select>';

答案 2 :(得分:0)

发送到服务器的唯一内容是value=""部分option中的内容。没有其他东西被发送。如果您确实需要发送这两个值,则可能需要将value更改为35,LS4。用逗号分隔两个值。然后在服务器端抓住它时,只需explode()将该值放入数组中并相应地使用第0和第1个元素。