943,172 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 399
  • PHP RSS
Aug 31st, 2010
0

How to take values from the DB to a combo box

Expand Post »
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

PHP Syntax (Toggle Plain Text)
  1. <select name="catId">
  2. <?php
  3. //get category id from the database
  4. $sql = "SELECT cat_id FROM tbl_category ORDER BY cat_id";
  5. $result = mysql_query($sql);
  6. while($row = mysql_fetch_array($result))
  7. {
  8. echo "<option value=\"".$row['cat_id']."\">".$row['cat_id']."</option> \n ";
  9. }
  10.  
  11. ?>
  12. </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..
Last edited by sami.asanga; Aug 31st, 2010 at 12:03 am.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
sami.asanga is offline Offline
35 posts
since Aug 2010
Aug 31st, 2010
0
Re: How to take values from the DB to a combo box
You need to add a WHERE statement to your query
for example if the cat id is called by example.com?catid=14
PHP Syntax (Toggle Plain Text)
  1. <?PHP
  2. $catid = $_GET['catid'];
  3. $catid = mysql_real_escape_string($catid);
  4. $sql = "SELECT cat_id FROM tbl_category WHERE cat_id = '$catid'";
  5. //rest of code
  6. ?>
Reputation Points: 13
Solved Threads: 34
Posting Whiz in Training
metalix is offline Offline
218 posts
since Mar 2010
Aug 31st, 2010
0
Re: How to take values from the DB to a combo box
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.
Reputation Points: 10
Solved Threads: 1
Light Poster
sami.asanga is offline Offline
35 posts
since Aug 2010
Sep 1st, 2010
0
Re: How to take values from the DB to a combo box
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"
Reputation Points: 13
Solved Threads: 34
Posting Whiz in Training
metalix is offline Offline
218 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Is it safe to use mysqli connection as global?
Next Thread in PHP Forum Timeline: URL Encoding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC