使用PHP和MYSQL基于表填充选择和输入框

时间:2012-07-05 13:12:28

标签: php mysql

我有一张表cene_pretplatne_stanarske,它有两列'lice'和'cene'。第一列'lice'应填充选择下拉菜单,另一列'cene'应根据下拉菜单的选择填充输入框。我试过这个:

<?php

mysql_connect('localhost', 'xxxxx', 'xxxxxxx');
mysql_select_db('xxxxxxx');
mysql_set_charset('utf8');
$sql = "SELECT * FROM cene_pretplatne_stanarske";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$cena = $row ["cena"];
$sql = "SELECT lice FROM cene_pretplatne_stanarske WHERE lice LIKE 'C0%'";
$result = mysql_query($sql);

echo "<select name='lice' onchange='document.getElementById(\'form1\').submit();'>";
while ($row = mysql_fetch_array($result)) 
{
echo "<option value='" . $row['lice'] . "'>" . $row['lice'] . "</option>";
}
echo "</select>";
echo "<input type='text' value='$cena' />";
?>

但它返回空的选择框,输入框的值为'cene'列的第一行。请帮忙。

1 个答案:

答案 0 :(得分:0)

检查返回的结果数量(如果有):

$i=0;

while ($row = mysql_fetch_assoc($result)) {
    echo "<option value='" . $row['lice'] . "'>" . $row['lice'] . "</option>";
    $i++;
}
echo $i

结束后

</select>