0

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.

3
  • 1
    Can you check and provide your generated HTML? By glancing at your code I noticed that you're not wrapping $ri['LoadNumber']'s value in quotes, for example. Commented Nov 3, 2015 at 2:32
  • you didn't close this echo "</select> " and make sure short tags are enabled. Error reporting would have signaled that "syntax" error which is off-topic. Commented Nov 3, 2015 at 3:16
  • This question has been viewed over 2500 times, and noone can give me a positive vote? this was a good question. Commented Mar 28, 2017 at 22:20

2 Answers 2

2

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

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

1 Comment

thanks all. also figured out I had it in body, moved it to header and it worked somewhat.
2
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.

2 Comments

add that to your answer and delete from the comments
This should have been the accepted answer: simple and more understandable than the accepted answer.

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.