根据mysql字段设置选定的选项

I have an HTML form that a user can use to edit a row in a mysql database, the PHP script queries the database and then displays the current data in form. I have this working fine for text fields with something like:

Correct Answer:<input type="text" name="answer" value="<?php echo $row['CorrectAnswer']; ?>"><br>

and simple radio set up like:

<?php 
            if ($row['Hazardous'] == "no"){
        ?>      
        Hazardous?:<input type="radio" name="hazardous" value="yes">Yes 
            <input type="radio" name="hazardous" value="no" checked="checked">No<br>
        <?php
            }else{
        ?>
        Hazardous?:<input type="radio" name="hazardous" value="yes" checked="checked">Yes   
            <input type="radio" name="hazardous" value="no" >No<br>
        <?php } ?>

I also have a select option element like below:

Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>

Which im trying to set up, I could use:

<?php if ($row['Category'] == "hazardawareness"){ ?>        
    Category: <select name="category">
                <option value="hazardawareness" selected="selected">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "observation"){ ?>
Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation" selected="selected">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "insurance"){ ?>
Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance"selected="selected">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "attitude"){ ?>
Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude" selected="selected">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "knowledge"){ ?>
Category: <select name="category">
                <option value="hazardawareness" >Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge" selected="selected">Gen. Knowledge</option>
            </select><br>   
<?php } ?>

But perhaps there is a better method to do this without duplicating so much code?

You can add an if clause on your option creation that will at the selected attribute if needed.

<?php $options = array( 
                 "Hazard Awareness" => hazardawareness, 
                 "Observation" => observation,
                 "Insurance" => insurance, 
                 "Attitude" => attitude 
              );  ?>
<select name="category">
<?php  foreach($options as $display => $value) {  ?>
    <option value='<?= $value ?>' <?php if($row['Category'] == trim($value)) { ?>selected='selected'<?php } ?>>
        <?= $display ?>
    </option>
<?php } ?>
</select>

I think this will help you to do the work using less code:

<?php
$options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
);
echo 'Category:  <select name="category">';
foreach($options as $display => $value) {
if ($row['Category'] == trim($value)) {
    echo '<option value="' . $value . '" selected>' . $display .'</option>';
}
else {
    echo '<option value="' . $value . '">' . $display . '</option>';
}
}
echo '</select>';

Put your option data in array().