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 ?> < Quote Link to comment 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> ?> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted April 24, 2007 Share Posted April 24, 2007 are you getting an error? Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 no but the listbox is empty Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
jlgray48 Posted April 24, 2007 Author Share Posted April 24, 2007 ok Quote Link to comment 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"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.