Use database data in a select (HTML / PHP)

0

With what tools can I integrate my database with an html / php, the intention is to create a field of type

<select>$opção1</select> <select>$opção2</select> <select>$opção3</select> <select>$...</select>

What I have in mind is to pull * from the database the name to put in the select, it would be an application to register a reference of a certain product, the select tab would inform the registered suppliers (through another tab) in the Bank of Data.

* pull with a command from sql select nome_fantasia from fornecedores;

    
asked by anonymous 14.11.2017 / 18:57

3 answers

0

To display database records within your select , you must use a PHP block for this. In this way, as I will show below, where value = 'nome_fantasia' will be stored inside the variable nome_fantasia which is the name of your select and this value will send the name of the provider

<select class="form-control" name="nome_fantasia">
  <option></option>
    <?php 

       $result = "SELECT nome_fantasia FROM fornecedores";
       $resultado = mysqli_query($conn, $result);

       while($row = mysqli_fetch_assoc($resultado)) {
         echo '<option value="'.$row['nome_fantasia'].'"> '.$row['nome_fantasia'].' </option>';
       }
    ?>
</select>
    
14.11.2017 / 19:47
1

Simple, do it and be happy:

     <?php $sql  = mysqli_query($conexao, "select nome_fantasia from fornecedores");?>
            <select><?php
              while($resultado = mysqli_fetch_array($sql)){ ?>     
                  <option value="<?=  $resultado['id'] ?>"><?php echo $resultado['campo_do_seu_banco']; ?></option>
                  <?php } ?>
            </select>
    
14.11.2017 / 19:10
0

Make a select in the table and use a foreach to display the names in select

<select>
<?php
foreach($select as $valor){
?>
  <option value="<?php echo $valor['nome']?>"><?php echo $valor['nome'] ?></option>
<?php
}
?>
</select>
    
14.11.2017 / 19:09