fill out combobox with database data

Asked

Viewed 2,862 times

0

I am following a video of php with mysql and I am able to list the data of a table in a list (list), just so you know that the problem of connection with the database is already overcome. I tried to use the same code to list the same table in a combobox, and it is not filling. The combo appears, but it does not fill in with the data. Does anyone know where I’m going wrong? Follow code:

<body>
    <ul>
        <?php
            // Passo 4 - Listagem dos dados
            while($registro = mysqli_fetch_assoc($categorias)){
        ?>
                <li><?php echo $registro ["nomecategoria"]?></li>
        <?php
            }

        ?>
    </ul>
    <form name="produto" method="post" action="">
         <label for="">Selecione um produto</label>
         <select>
         <option>Selecione...</option>

         <?php 
         while($registro = mysqli_fetch_assoc($categorias)) { ?>
         <option = "<?php echo $registro['categoriaID'] ?>"><?php echo $registro["nomecategoria"] ?></option>
         <?php } ?>

         </select>
    </form>
                <?php
        //Passo 5 - Liberar dados da memória
        mysqli_free_result($categorias);
    ?>
</body>
  • Put all the code so that it is possible to analyze the question better

  • Put your query query to the bank as well, so you want us to resolve how?

  • Welcome Neto Sales, be sure to read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

2 answers

0

True. It was time to copy that you jumped. But I want to emphasize that the consultation to the bank is all right, because the list above the combo is filling normally.

 <body>
    <ul>
        <?php
            // Passo 4 - Listagem dos dados
            while($registro = mysqli_fetch_assoc($categorias)){
        ?>
                <li><?php echo $registro ["nomecategoria"]?></li>
        <?php
            }

        ?>
    </ul>
    <form name="produto" method="post" action="">
         <label for="">Selecione um produto</label>
         <select>
         <option>Selecione...</option>

         <?php 
         while($registro = mysqli_fetch_assoc($categorias)) { ?>
         <option = "<?php echo $registro['categoriaID'] ?>"><?php echo $registro["nomecategoria"] ?></option>
         <?php } ?>

         </select>
    </form>
                <?php
        //Passo 5 - Liberar dados da memória
        mysqli_free_result($categorias);
    ?>
</body>
  • What is this answer? Is one worth seeing again? This field is used for answers! You can edit your question by clicking edit just below the tags.

0

It turns out that fetch_array/fetch_assoc "consumes" all lines in the first while, that is to change the internal pointer until the end of the data during the while. That way you need to return the ponteiro for the first line placing mysqli_data_seek($categorias, 0); before the second while

..................
..................
<form name="produto" method="post" action="">
     <label for="">Selecione um produto</label>
     <select>
     <option>Selecione...</option>

     <?php 
     mysqli_data_seek($categorias, 0);
     while($registro = mysqli_fetch_assoc($categorias)) { ?>
     <option = "<?php echo $registro['categoriaID'] ?>"><?php echo $registro["nomecategoria"] ?></option>
     // o correto acima é <option VALUE = "<?php ......
     // FALTOU O value

     .................
     .................

mysqli_data_seek

You could also use just one while to obtain the same result:

<ul>
    <?php
        // Passo 4 - Listagem dos dados
        while($registro = mysqli_fetch_assoc($categorias)){

            echo "<li>". $registro ["nomecategoria"] . "</li>";
            //concatena os registros retornados
            $paraSelect .= "<option value='" . $registro["categoriaID"] ."'>" .$registro["nomecategoria"] . "</option>";

        }

    ?>
</ul>
<form name="produto" method="post" action="">
     <label for="">Selecione um produto</label>
     <select>
     <option>Selecione...</option>

     <?php 
      //imprime os options
      echo $paraSelect; 
     ?>

     </select>
</form>
<?php
    //Passo 5 - Liberar dados da memória
    mysqli_free_result($categorias);
?>

Browser other questions tagged

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