I have been trying to run a mySQL query that populates a HTML form where I present a tag. I would like to populate both the value and the text from the mySQL table.
Here is what I tried based on finding things on different web sites, and it did not work
<select name="id">
<?php
require_once dirname(__FILE__) . '/db_connect.php';
$db = new DB_CONNECT();
$query="SELECT id, name FROM file order by name";
$result=mysql_query($query) or die ("Query to get data from firsttable failed: " .mysql_error());
while ($row = mysql_fetch_array($result)) {
$id = $row[id];
$name =$row[name];
echo "<option value=\"$id\">
$name
</option>";
}
?>
</select>
Comment:
Did not work refers to the fact that the select drop down is empty.
I did pull the PHP out of the form and put it in it’s own name.php file to try and run it. it seemed to work. So I added echo statements in the php to echo out the start and end of the form, and the initial select line. This worked just great, no problems, the drop down was populated just fine.
Back on the HTML form above, I did add a few other echo statements in the PHP to output some debug statements. The firs echo is always ignored, it does not add any select item. the other echo lines only adds the text specified, variables are output as the variable name, not the content. so $name shows up in the select drop down.
You need to put single quotes around id and name. That is you need to replace this
by this
without knowing what’s in DB_CONNECT and what your error is this is my best guess at what’s wrong.