<?php

$query=mysqli_query($con,"select * from reg where user_id = '".$_GET['id']."' ");
$row=mysqli_fetch_array($query);
?>

<select name="sex" id="opt2">
<option value="">select</option>
<option value="Male" selected="selected">Male</option>
<option value="Female" selected="selected">Female</option>
</select>

Dani AI

Generated

For a single-select dropdown, only one option should be marked selected. Use the value from your database to decide which option gets selected; if no value exists, keep the placeholder selected. Also escape both option values and labels to avoid XSS, and avoid hardcoding multiple selected attributes.

Example renderer pattern:

$sex = isset($row['sex']) ? (string)$row['sex'] : '';
$options = ['' => 'select', 'Male' => 'Male', 'Female' => 'Female'];

echo '<select name="sex" id="opt2">';
foreach ($options as $value => $label) {
    $isSelected = ($value === $sex) ? ' selected' : '';
    printf(
        '<option value="%s"%s>%s</option>',
        htmlspecialchars($value, ENT_QUOTES, 'UTF-8'),
        $isSelected,
        htmlspecialchars($label, ENT_QUOTES, 'UTF-8')
    );
}
echo '</select>';

Security and correctness tips:

  • Never concatenate $_GET['id'] into SQL. Use prepared statements with bound parameters (PDO or mysqli). See PHP PDO prepared statements. If the id is numeric, validate with filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT).
  • Do not mix mysql_* with mysqli_*. The mysql_* extension is removed as of PHP 7; migrate to PDO or mysqli. Reference: PHP manual: MySQL (original) extension.
  • Ensure option values match what you intend to submit (e.g., the actual state_id or the year number), not a previously selected value. More on the select element and selected attribute in the HTML spec: WHATWG select element. Also see htmlspecialchars for safe output.

Recommended Answers

All 4 Replies

Something like this might work.

<?php foreach($row as $reg): ?>
    <select name="sex" id="opt2">
    <option value="">select</option>
    <option value="Male" <?php if($reg['sex'] == 'Male'){echo 'selected="selected"';}?>>Male</option>
    <option value="Female" <?php if($reg['sex'] == 'Female'){echo 'selected="selected"';}?>>Female</option>
    </select>
<?php endforeach; ?>

Thank you...

<select name="state_id" id="states" class="form-control">
<?php 
$sql10= mysql_query("select * from states");
while($row10 = mysql_fetch_array($sql10)){
?>
<!-------------------------------edit-code----------------------------->
<option value="<?php echo $row10['state_id']; ?>"
<?php 
if(($row7['state_id'] == $row10['state_id'])){
echo 'selected = "selected"';
} ?>>
<?php echo $row10['state_name']; ?></option>
<?php
}
?>
</select>
<select name="yr">
<?php 
for($i=2005;$i<=date('Y');$i++){ ?>
?>
<!-------------------------------edit-code----------------------------->
<option value="<?php echo $row1['year1']; ?>"
<?php 
if(($row1['year1'] == $i)){
echo 'selected = "selected"';
} ?>>
<?php echo $i; ?></option>
<?php
}
?>
</select>
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.