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>

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

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.