我是 PHP/MySQL 新手,我有一个问题。如果我想修改一个 php 文件,在其中创建一个人员列表,因此我想在表单中创建一个动态下拉列表(使用数据库另一个表中的数据),我该如何使用它?我尝试过,但失败了。当我尝试检索表单外的数据时,没问题,但是当我尝试将命令重新定位到表单中时,什么也没有发生!这是为什么?在表单中,是否有局部变量或类似的东西?
这是我的简化代码
database_connect();
...
html blah blah
...
$result0 = mysql_query("SELECT * FROM depts",$link)or die(mysql_error());
while($row1 = mysql_fetch_array($result0))
echo $row1['dep'];// i can see all the contents of row1, but ...
<form action="add_department.php" method="post">
<tr><td>dep.:</td><td><select name="dep" value="<?php echo $dep; ?>">
<?php
//...if i move the commands here (inside the form)
$result0 = mysql_query("SELECT * FROM depts",$link)or die(mysql_error());
while($row1 = mysql_fetch_array($result0))
echo $row1['dep'];//shows nothing to the screen. Why??
<tr><td><input type="submit" name="submit" value="submit"></td></tr>
</form>
php_error_log文件中没有记录
最佳答案
这个问题很模糊,因为你没有提到它是如何下降的,但我注意到了一些事情:
HTML SELECT 标签不使用 value 属性,您需要向 OPTION 添加 SELECTED 属性,例如
<select name="dep">
<?php $result0 = mysql_query("SELECT * FROM depts",$link)or die(mysql_error());
while($row1 = mysql_fetch_array($result0))
{?>
<option value="<?php echo $row['dep']; ?>"<?php echo $row['dep'] == $dep ? ' SELECTED':'' ?>><?php echo $row['dep'] ?></option>
<?php } ?>
</select>
此外,您在标记后缺少结束语。
关于php - 检索 php/mysql 内动态下拉列表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19202325/