0

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

6
  • 1
    Please share the code. Commented Oct 5, 2013 at 20:26
  • 1
    Please provide a short but complete example which demonstrates the problem. Make sure you include the code! Commented Oct 5, 2013 at 20:28
  • "Failing" is when you don't show code of what you tried. Success comes from failure. How do you think you're able to see where you're going at night? ;-) Commented Oct 5, 2013 at 20:32
  • @neeagl I made the changes! Commented Oct 7, 2013 at 16:24
  • @Marty McVry I made the changes! Commented Oct 7, 2013 at 16:24

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.