jlgray48 Posted April 24, 2007 Share Posted April 24, 2007 I'm trying to create a php script that creates a listbox and populates the list with values from a table named Inventorytable. I only need the values from the ItemName field to be populated in the listbox. Here is what I have so far....... <? $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn) or die ("error cannot connect"); $sql = "select itemname from inventorytable;"; $result = mysql_query($sql,$conn); while ($array = mysql_fetch_array($result)) { print $array[itemName];} <html> <SELECT > <OPTION VALUE="01">Option 1 <OPTION VALUE="02">Option 2 <OPTION VALUE="03">Option 3 </SELECT> </html ?> < Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/ Share on other sites More sharing options...
benjaminbeazy Posted April 24, 2007 Share Posted April 24, 2007 <? $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn) or die ("error cannot connect"); $sql = "select itemname from inventorytable"; $result = mysql_query($sql,$conn); ?> <select name="item"> <? while ($array = mysql_fetch_array($result)) { ?> <OPTION VALUE="<?=$array['itemname'];?>"><?=$array['itemname'];?></option> <? } ?> </SELECT> </html> ?> Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236689 Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 That doesn't work either. I'm trying to follow your code but having a hard time. Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236693 Share on other sites More sharing options...
benjaminbeazy Posted April 24, 2007 Share Posted April 24, 2007 are you getting an error? Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236694 Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 no but the listbox is empty Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236696 Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 Okay, im assuming you have an ID field for each item in the inventory table called itemid. Change the field name as necessary. I have also added "or die(mysql_error());" after your query for debugging purposes. <select name="items> <? $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn) or die ("error cannot connect"); $sql = "SELECT itemid, itemname FROM inventorytable;"; $result = mysql_query($sql,$conn) or die(mysql_error()); while ($array = mysql_fetch_array($result)) { $itemId = $array['itemname']; $itemName = $array['itemname']; ?> <option value="<?=$itemId?>"><?=$itemName?> <? } ?> </select> Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236702 Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 I have a table named inventorytable with a field named ItemName Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236706 Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 In that case, just this <select name="items"> <? $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php012",$conn) or die ("error cannot connect"); $sql = "SELECT * FROM inventorytable"; $result = mysql_query($sql,$conn) or die(mysql_error()); while ($array = mysql_fetch_array($result)) { $itemName = $array['itemname']; ?> <option value="<?=itemName?>"><?=$itemName?> <? } ?> </select> Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236709 Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 It looks right but it isn't working you are losing me with all the ?> throughout. The help is greatly appreciated Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236712 Share on other sites More sharing options...
benjaminbeazy Posted April 24, 2007 Share Posted April 24, 2007 those are called short tags and php code goes inside them Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236714 Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 ok Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236718 Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 It may be that short tags arent enabled on your server Try replacing the opening tags <? with <?php Leave the closing tags ?> as they are putting something like <?=$myVar?> is a shortcut way of putting <?php echo "$myVar"; ?> Link to comment https://forums.phpfreaks.com/topic/48406-php-code-to-populate-listbox-with-values-from-database/#findComment-236719 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.