Musicdad
Musicdad

Reputation: 55

Create and fill combo box with mysql data with php

I have done google searches and found several answers but I cannot get this to work. I am trying to read a field from ym mysql database and create and populate a combo box containing those values. The database connection works but all I get is a list of the values and no combo box. I am posting my code, along with the results.

<?php
$cn=mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("db_name",$cn) or die(mysql_error());
$sql = "SELECT LoadNumber FROM tblLoads";
$result = mysql_query($sql);
?>
<select name="loads" size=1>
<?
while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";
}
echo "</select> "
?>

Here is my output:

1637825
1637933
1638102
1638109
1638574
1638683
>

Please, this is driving me crazy, i don't see what I am doing wrong. it does not create the combo box.

Upvotes: 0

Views: 17745

Answers (2)

Kuya
Kuya

Reputation: 7310

You are complicating your code! Some part of it is wrapped in PHP and part of it is not.

Change this...

<select name="loads" size=1>
<?
while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";
}
echo "</select> "
?>

To this...

<select name="loads" id="loads">
<?php while($ri = mysql_fetch_array($result)) { ?>
<option value="<?php echo $ri['LoadNumber'] ?>"><?php echo $ri['LoadNumber'] ?></option>
</select>
<?php } ?>

Finally... you should stop coding in mysql_ !!! It has been deprecated and will soon cease to exist. When that happens, all of your sites using mysql_ code will stop functioning.

You should really be learning pdo_mysql

Upvotes: 2

kenphor
kenphor

Reputation: 32

Try this.
<select name="loads" size=1>
<?php while($ri =  mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";?>
<select>

just move the last select from the php script.

Upvotes: 2

Related Questions