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

Dani AI

Generated

Short answer: the dropdown must be told which option is the current one. Populate the list of categories (all rows), load the record being edited with a WHERE on its primary key (as suggested), then when generating each <option> compare the category id to the record’s saved category id and add the selected attribute for the match. Use prepared statements and escape output to avoid SQL injection/XSS.

Example (PDO) — fetch the item first, then all categories and mark the matching option:

// (assume $pdo is a PDO connection)
$itemId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// load the item being edited (WHERE on the item, not on the categories list)
$stmt = $pdo->prepare('SELECT id, cat_id FROM items WHERE id = ?');
$stmt->execute([$itemId]);
$item = $stmt->fetch(PDO::FETCH_ASSOC);

// load all categories
$cats = $pdo->query('SELECT cat_id, name FROM tbl_category ORDER BY name')->fetchAll(PDO::FETCH_ASSOC);
?>

<select name="catId">
<?php foreach ($cats as $c):
    $selected = ($item && $c['cat_id'] == $item['cat_id']) ? ' selected="selected"' : '';
?>
    <option value="<?php echo htmlspecialchars($c['cat_id']); ?>"<?php echo $selected; ?>>
        <?php echo htmlspecialchars($c['name']); ?>
    </option>
<?php endforeach; ?>
</select>

Troubleshooting notes: if adding a WHERE made the dropdown empty, the WHERE was likely applied to the categories query by mistake — the categories list should not be filtered; the WHERE belongs to the query that loads the record to edit. Also check types when comparing (string vs int), prefer == or explicit casts, and when redisplaying after a failed submit prefer posted value ($_POST['catId']) over DB value. Finally, avoid deprecated mysql_* APIs; use PDO or mysqli with prepared statements as shown. This resolves ’s issue where the browser always showed the first option.

Recommended Answers

All 3 Replies

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
?>

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.

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"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.