0

我是 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 文件中没有记录

4

1 回答 1

1

The question is a big vague as you don't mention how it is falling, but a few things I've noticed:

The HTML SELECT tag does not use a value attribute, you need to add a SELECTED attribute to an OPTION, e.g.

<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>

Also, you are missing a closing after the tag.

于 2013-10-05T22:51:18.593 回答