# Data from MySQL into an PHP dropdown box

**URL:** <https://www.phphelp.com/t/data-from-mysql-into-an-php-dropdown-box/24189>\
**Category:** General Database (Oracle, SQL Server, etc)\
**Created:** [April 16, 2015, 2:09am UTC](https://www.phphelp.com/t/data-from-mysql-into-an-php-dropdown-box/24189 "2015-04-16T02:09:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![r-man](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@r-man](https://www.phphelp.com/u/r-man)\
**Post date:** [April 16, 2015, 2:09am UTC](https://www.phphelp.com/t/data-from-mysql-into-an-php-dropdown-box/24189/1 "2015-04-16T02:09:48Z")

</div>

I am having trouble getting all data from MySQL database table into a drop-down form.

I have a table in a database that has four columns like this this example:  
Products table:  
Salesman id – which is a foreign key  
Product id – which is a foreign key  
Quantity  
Date –timestamp

I am trying to build an insert, edit and delete form in PHP. I want to be able to connect to the database table and have the salesman id and the product id update in an html drop-down box. So if there are nine salesmen with an id of 1 through 9. I want to be able to click on the selection tab and choose which nine salesman id I want - same for the product id. One salesman can have different product ids attached to them. I know in phpMyAdmin you can select a table and click on insert and see the Value dropdown button with all the nine salesmen to insert. When I try to code to get all the salesman ids I only get eight. I am not sure what I am missing. If someone can help me I appreciate it.  
Thanks,  
r.

Here is my code I have so far.  
This code will only display eight of the nine salesman ids – I need all nine. I need to do the same for product id.

[php]

```
    $query = "select DISTINCT salesmanid from products";
 $result = $db->query($query);
   $numrecs = $result->num_rows;
echo '<select name = "salesmanid">';
echo "Select an option</option>";
 for ($i = 0; $i < $numrecs; $i++ ) {
$row = $result->fetch_assoc();
echo '<option value ="'.$row[' salesmanid '].'">'.$row[' salesmanid '].'</option>';
 }
 echo '</select>';

```

[/php]

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [April 16, 2015, 3:28pm UTC](https://www.phphelp.com/t/data-from-mysql-into-an-php-dropdown-box/24189/2 "2015-04-16T15:28:13Z")

</div>

Something, similar or exactly to this should get you going.

[php] $query = “select DISTINCT salesmanid from products”;  
$result = $db-\>query($query);  
$numrecs = $result-\>num\_rows;  
echo ‘’;  
echo “Select an option”;

while($row = $result-\>fetch\_assoc()) {

echo ‘’.$row[’ salesmanid ‘].’’;

}

echo ‘’;[/php]

---

<div class="post-metadata">

**Author:** ![r-man](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@r-man](https://www.phphelp.com/u/r-man)\
**Post date:** [April 17, 2015, 5:35pm UTC](https://www.phphelp.com/t/data-from-mysql-into-an-php-dropdown-box/24189/3 "2015-04-17T17:35:17Z")

</div>

Topcoder:

Thanks for your reply. I appreciate your code. I figured it out. I was pulling from the wrong table. I got my code to work.

Thank you.  
r.
