put MYSQL data in my form combobox using class and php

Asked

Viewed 330 times

-1

I want to load in the COMBOBOX of the user registration maintenance form the user NAME data coming from MYSQL. I have a method below that does the query in mysql database and returns an array

public function LerDadosUsu(){
    try{
        $lerusu = "SELECT * FROM usuario ";
        $listusu = $this->con->Connect()->prepare($lerusu);
        $listusu->execute();
        $retDados = $listusu->fetchAll(PDO::FETCH_ASSOC);
        return $retDados;
        }
     catch(PDOException $erro_2){
     echo 'erro'.$erro_2->getMessage();
        }

    }

I have the form below that I want to show this information, but where this the combobox option value does not bring the information that already exists in mysql

      <form method="post" name="frm_usuario">

                      <div class="form-group">
                                          <?php    

                                         echo '<label for="filter">Filtrar Usuário</label>

                                         <div class="input-group"><span class="input-group-addon"><i class="fa fa-search"></i></span><select class="form-control">
                                         /*crio o objeto para buscar valor*/
                                         $objeto = new Usuario();
                                         $usuario = $objeto->LerDadosUsu();

                                         /*crio um foreach para retornar o campo*/   
                                         foreach($usuario as $usuario):

<option value="<?$usuario['.nom_usuario.'];?>"selected>Escolha o usuario</option>
                                         endforeach;
                                         </select>
                                        ?>

                       </div>
                       </div>

The error that appears in my form when I am running is this below

ERROR: Use of Undefined Constant nom_usuario - assumed 'nom_usuario' in C:.. user.php on line 327

  • Always add the language tag to the question, this is key to index the question and to make it easy for tag respondents.

1 answer

0


You need to fix the code. in this excerpt foreach($usuario as $usuario): <option value="<?$usuario['.nom_usuario.'];?>"selected>Escolha o usuario</option>you’re mixing PHP and HTML.

<form method="post" name="frm_usuario">
    <div class="form-group">
    <label for="filter">Filtrar Usuário</label>
        <div class="input-group"><span class="input-group-addon"><i class="fa fa-search"></i></span>
            <select class="form-control">
            <?php
            /*crio o objeto para buscar valor*/
            $objeto = new Usuario();
            $usuario = $objeto->LerDadosUsu();
            /*crio um foreach para retornar o campo*/   
            foreach($usuario as $usuario):
            ?>
                <option value="<?php echo $usuario['.nom_usuario.'];?>"selected>Escolha o usuario</option>
            <?php
            endforeach;
            ?>
            </select>
        </div>
    </div>

Browser other questions tagged

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