0

so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.

here is my code:

<?php 
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>

The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.

I would appreciate anyones thoughts :D

thanks

3
  • 1
    as bensiu points out below, you are not appending to your string, you're overwriting each time. But if you're doing this multiple places, why isn't it a function that you pass an array to and get the HTML back from? Commented Mar 21, 2011 at 13:34
  • Do a sanity check... how many rows are being returned by the query? Run the exact query against mysql directly -- do you get the results you expect? Commented Mar 21, 2011 at 13:36
  • Is the user_id attribute of the units_tb table an int or a string? Commented Mar 21, 2011 at 13:37

2 Answers 2

4
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";

add . before = and initiate $aa = ''; before while loop

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

2 Comments

Good eye... a classic syntax mistake.
apologies, i do now feel like a fool. it was just the . thanks though!
0
<?php 
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
    $options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>

should work. You forgot a . in your while loop

1 Comment

and $aa has not been initiated

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.