Forum Moderators: coopster

Message Too Old, No Replies

Populating a Select box

         

Esqulax

7:49 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



Hiya, i did a search and found a great bit o code for making Data in a database become elements in a select box.

I have a table called countries, and the row is called country_name (in which ive listed about 4 countries so far


<form action="<?php echo($self);?>" method="post">

Forename:<input type="text" name="fname" size="8">
Surname:<input type="text" name="sname" size="8">
<br><br>

<?php
$sql ="SELECT country_name FROM countries";
$query = mysql_query($sql);

echo '<select name="cntry">';
while
($row = mysql_fetch_array($query))
{
echo '<option value="',$row['country_name'],'">',$row['country_name'],'</option>';
}
echo '</select>';
?>
<!-- Country:<input type="text" name="cntry" size="8">-->

Gender:<input type="text" name="sex" size="8">
<input type="submit" value="Add to database"> </form>

The select box appears, but with all options being "0"
Any ideas?

Esqulax

8:02 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



OK ok ok ok... hold the phone,
Was me being stupid.

Turns out when i created the table, i'd set the TYPE to INT, so anything other than numbers that i entered came up as 0.

The code above works great! (I think i added $conn in: $sql ="SELECT country_name FROM countries";
$query = mysql_query($sql,$conn);
)

So.. nothing to see here, look the other way ;)