I think the problem lies here
echo "<option value='".trim($row['deptcode_fk'])."'>".$row['deptcode_fk']."</option>";
there is a space before and after the value is concatenated to value property.
If the problem persists, try printing the selected value on html and see whats the problem