Select dropdown list value depending on db value
<p>I would like an item in a dropdown list to be selected if the value corresponds with a value in a database.</p>

<p>Here is my code so far...</p>

<pre><code>$hardware_type = $row['type'];

//pull names from hardware types from databse
$sql = "SELECT * FROM s_num_serialz_types ORDER BY type ASC";
$result = $mysqli-&gt;query($sql) or die($mysqli-&gt;error.__LINE__);
$type_dropdown = '&lt;select id="serial_type" class="accounts_input_dropdown" type="text" name="serial_type" selected="'.$_POST['serial_type'].'"/&gt;&lt;option&gt;&lt;/option&gt;';
while($row = mysqli_fetch_assoc($result)){
    if($row['type'] != $hardware_type){
    $type_dropdown .= "\r\n&lt;option value='{$row['type']}'&gt;{$row['type']}&lt;/option&gt;";
    }else{
        $type_dropdown .= "\r\n&lt;option value='{$row['type']} selected=\"selected\"&gt;{$row['type']}&lt;/option&gt;";
    }
}
$type_dropdown .= "\r\n&lt;/select&gt;";
</code></pre>

<p>I have tried several ways of writing this code and none seem to work.  The first time I tried...</p>

<pre><code>while($row = mysqli_fetch_assoc($result)){
    if($row['type'] == $hardware_type){
    $type_dropdown .= "\r\n&lt;option value='{$row['type']} selected=\"selected\"&gt;{$row['type']}&lt;/option&gt;";
    }else{
        $type_dropdown .= "\r\n&lt;option value='{$row['type']}'&gt;{$row['type']}&lt;/option&gt;";
    }
}
</code></pre>

<p>This selected the correct item in the list but the rest of the form containing this list doesn't show.</p>

<p>Any help will be greatly appreciated</p>
