Populate html select with database values

Asked

Viewed 77 times

0

Good morning, I’m a beginner in php and I’m creating a personal project to register people. I have a screen that lists all users and when clicking change user, it returns the values of the user in the database, but it does not return the sex of the user. I want to know how to automatically fill the html select according to the database value.

  • 2

    It is important to ask the question the code and what values comes from the bank related to sex.

1 answer

0

Within the select you have the options you must do an if comparing with the bank return information. Ex: Whereas you mount a select with a gender table with the fields type={M,F} and name={Masculine, Feminine} or within an array. And in the person’s registration have a generic field that you store the type.

<select name='genero'>
  <?php foreach($generos as $genero):?>
     <option value="<?=$genero['tipo']?>"
     <?php if ($genero['tipo'] == $pessoa['genero']) {?> selected="selected" <?php }?>>
     <?= $genero['nome']?></option>
  <?php endforeach;?>
</select>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.