Jump to content

[SOLVED] populating dropdown list from mysql


edte

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.