954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to take values from the DB to a combo box

Hi,

I need to get the itemId's from the Database to a combo box where the combo box will increase according to the new values.

I wrote a code like this

<select name="catId">
                        <?php
                          //get category id from the database 
                            $sql = "SELECT cat_id FROM tbl_category ORDER BY cat_id";
                            $result = mysql_query($sql);
                            while($row = mysql_fetch_array($result))
                            {
                                 echo "<option value=\"".$row['cat_id']."\">".$row['cat_id']."</option> \n  ";
                            }
                
                        ?>				    
                      </select>


With that I could have taken the item id and the other necessary values displayed according to the id.But the problem is even though it displayed correct values ,always the item id which is selected is the first one.So I cant do any update with it,because it will update only the 1st record in the DB table since update query is working according to the itemId.How should I change the code to get the correct item id selected with its values displayed in the form where I can edit and update it.

Please help me..

sami.asanga
Light Poster
35 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

You need to add a WHERE statement to your query
for example if the cat id is called by example.com?catid=14

<?PHP
$catid = $_GET['catid'];
$catid = mysql_real_escape_string($catid);
$sql = "SELECT cat_id FROM tbl_category WHERE cat_id = '$catid'";
//rest of code
?>
metalix
Posting Whiz in Training
218 posts since Mar 2010
Reputation Points: 13
Solved Threads: 34
 

No..Its not working..because when we give a where clause in the select statement none of the category id get selected.Just only the combo box will display with out any value.

sami.asanga
Light Poster
35 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

So reading your problem from a different angle. are you only recieving one result from the db in your first example?
or are you needing selected="selected"

metalix
Posting Whiz in Training
218 posts since Mar 2010
Reputation Points: 13
Solved Threads: 34
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You