0

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.

5
  • 1
    "did not work" how? You get errors? Script crashes? Bad output? You are missing ' quotes on your $row[id] and $row[name] bits, but usually that just issues a warning in PHP. Commented Sep 24, 2012 at 4:40
  • 1
    please dont use the mysql_* use insted the mysqli or PDO Commented Sep 24, 2012 at 4:41
  • sorry for that sql injunction the user doesnot getting data from browser Commented Sep 24, 2012 at 4:44
  • also use the { } in echo if you using like that echo "<option value=\"{$id}\">{$name}</option>"; Commented Sep 24, 2012 at 4:45
  • OK, the "Did not work" means that the dropdown for the select is empty. I pulled the php code out and ran it in a name.php and the PHP code worked. I even added code to that to echo out the start and end of the form, and It looked great. When I add the php code back into the HTML form to just populate the select I get nothing in the select drop down. I did test a little more there, by adding a few other PHP echo statements in the format of adding a select item, and the first echo statement is not added, and the rest of them only inserts text, so $name shows up as $name in the drop down. Commented Sep 24, 2012 at 12:06

1 Answer 1

5

You need to put single quotes around id and name. That is you need to replace this

$id = $row[id];
$name =$row[name];

by this

$id = $row['id'];
$name =$row['name'];

without knowing what's in DB_CONNECT and what your error is this is my best guess at what's wrong.

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

2 Comments

I concur, unlikely that those are meant to be constants.
I am sure that helped, but did not fix my problem. See comment above.

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.