How to make the drop down option selected with database value using php

I’m having a select option in which values are populating from the database. During updation I need to get that value selected.How can I do that?Please help.

Here is my code for select tag

<select name="supplier_id" class="form-control form-control-sm" id="colFormLabelSm">
<option value="0" selected="selected" disabled="disabled">
Select supplier
</option>
<?php
$supp_select="SELECT supplier_id,supplier_name FROM supplier_details";
$supp_query=mysqli_query($con,$supp_select);
while ($supp_data=mysqli_fetch_assoc($supp_query)) 
{
?>
<option value="<?php echo $supp_data['supplier_id']?>">
<?php echo $supp_data['supplier_name'];?>
</option>
<?php
}
?>
</select>

You can compare database value with some variable or $_POST value like

<option value="<?php echo $supp_data['supplier_id']?>" <?php if ($_POST['input-name'] == $supp_date['supplier_name']) echo 'selected'; ?>><?php echo $supp_data['supplier_name'];?></option>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.