简体   繁体   中英

retrieve data for dynamic drop down inside php/mysql

I'm new in PHP/MySQL and I have a question. In case I want to modify a php file in which i create a list of people so i want to create a dynamic drop down list (with data from another table of the database) in the form, how can I use that? I tried but I failed. When Ι try to retrieve the data outside the form, it's OK, but when I'm trying to relocate the commands into the form this time, nothing happens! why is that? In the form, are there local variables or sth like that?

Here is my simplified code

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>

There are no records in the php_error_log file

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM