edte Posted November 15, 2008 Share Posted November 15, 2008 i'm new with php, can someone help me on how can i populate a dropdown list from my table(mysql). What did i miss it's i've got a dropdown list but no data shown. here's my code : <Select name = 'selected'> <? $LINK=MYSQL_CONNECT("LOCALHOST","admin","admin"); IF (!$LINK) DIE ('COULD NOT CONNECT TO MYSQL'); $DB=MYSQL_SELECT_DB("invty"); IF (!DB) PRINT("COULD NOT CONNECT"); $QUERY ="Select Stock_No, Stock_Name, Quantity,Balance From StockIn"; $RESULT= MYSQL_QUERY($QUERY); while ($res = mysql_fetch_array($RESULT)) { echo "<option value=\"{$res['Stock_No']}\" />{$res['Stock_Name']}</option>\n"; } ?> </Select> Quote Link to comment Share on other sites More sharing options...
jonaHill87 Posted November 15, 2008 Share Posted November 15, 2008 I think it's because you didn't concatenate the values properly. I could be wrong though. echo "<option value='".$res['Stock_No']."' />".$res['Stock_Name']."</option>\n"; Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted November 15, 2008 Share Posted November 15, 2008 echo "<option value=\"".$res['Stock_No']."\" />".$res['Stock_Name']."</option>\n"; you need to backslash html outputed double quotes, double quotes are significant in php, you also miss out the concatinator symbol which is the dot '.', in C++ its >> and in some languages + in php its teh dot Quote Link to comment Share on other sites More sharing options...
chronister Posted November 15, 2008 Share Posted November 15, 2008 This is how I would go about accomplishing this.... <?php $LINK = MYSQL_CONNECT("LOCALHOST","admin","admin"); IF (!$LINK) DIE ('COULD NOT CONNECT TO MYSQL'); $DB = MYSQL_SELECT_DB("invty"); IF (!DB) PRINT("COULD NOT CONNECT"); $QUERY ="Select Stock_No, Stock_Name, Quantity,Balance From StockIn"; $RESULT= MYSQL_QUERY($QUERY); if(mysql_num_rows($RESULT) < 1) { echo 'No results returned from the database'; // added error message if no data returned from the server. } ?> <Select name = 'selected'> <?php while ($res = mysql_fetch_array($RESULT)) { echo '<option value="'.$res['Stock_No'].'" />'.$res['Stock_Name'].'</option>\n"'; } ?> </Select> I find that that escaping a crap load of stuff makes for really messy coding and is harder to spot problems. I like to use single quotes for most everything and concatenate strings properly. It makes the actual variable stand out more and is easier to see. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 15, 2008 Share Posted November 15, 2008 The original echo statement is valid, as are the two three that have been posted since. Text that is output inside of html tags, unless it occurs in the tag where normal content would be output to the browser, will not appear, but will be present in the "view source" of the page. Do a "view source" of the page in your browser to view if any of the error reporting in the logic is giving any messages. Also, you should be learning php, developing php code, or debugging php code on a system where error_reporting is set to E_ALL and display_errors is set to ON to get php to help you. Quote Link to comment Share on other sites More sharing options...
.josh Posted November 15, 2008 Share Posted November 15, 2008 well he does have a var between his option tags... alter this line of code to the following: $RESULT= MYSQL_QUERY($QUERY) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
edte Posted November 17, 2008 Author Share Posted November 17, 2008 Thanks guys for your help. i appreciate it very much. by the way my code is now working. again thanks you. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted November 17, 2008 Share Posted November 17, 2008 Thanks guys for your help. i appreciate it very much. by the way my code is now working. again thanks you. can u please tell us which one solved your issue because we will all be slain and the correct answer person will be allowed to live on. Oh and mark the topic solved only you have the power to do that. 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.