Return Database Data and List in Combobox

Asked

Viewed 1,059 times

0

Hello, I would like to be able to return the values of the company table database and for each row that returns go creating an option in the combobox, I would like to know how to do this, I think should be with while or for each, but would like help to assemble.

Code of the Comboxbox:

<div class="profile-info-row">
<div class="profile-info-name"> Empresa </div>  

<div class="profile-info-value">
    <select name="empresa_destino" id="form-field-1" class="col-xs-11 col-sm-6 col-md-8">
        <option value="">Selecione a Empresa</option>
        <option value="1">Emp1</option>
        <option value="2">Emp2</option>
        <option value="3">Emp3</option>
    </select>
</div>
</div>

PHP code to return database values:

<?php
    session_start();

    if (!isset($_SESSION["logado"]) || $_SESSION["logado"] != TRUE) {
        header("Location: login.html");
    }

        include "conexao.php";
        include "executaSQL.php";

        $SQL = "SELECT cod_empresa , razao_social FROM `tbl_empresa`";

        $link= conectar ();
        $inserido = executaSQL($SQL, $link);

?>

EDIT:

I managed to adjust that code of mine:

    <div class="profile-info-value">
                                        <?php
    include "conexao.php";
    include "executaSQL.php";

    $link=conectar(); 

    $busca_query = mysql_query("SELECT cod_empresa,razao_social FROM tbl_empresa")or die(mysql_error());
?>                                        
                                            <select name="empresa_destino" id="form-field-1" class="col-xs-11 col-sm-6 col-md-8">
                                                <option value="" selected disabled="disabled" hidden>Selecione a Empresa</option>

                                                <?php while ($dados = mysql_fetch_array($busca_query)) { ?>

                                            <option value="<?php echo $dados['cod_empresa']; ?>"><?php echo $dados['razao_social']; ?></option> <?php } ?>

                                            </select>
                                        </div>

2 answers

0


Resolution Code I used:

<div class="profile-info-value">
                                        <?php
    include "conexao.php";
    include "executaSQL.php";

    $link=conectar(); 

    $busca_query = mysql_query("SELECT cod_empresa,razao_social FROM tbl_empresa")or die(mysql_error());
?>                                        
                                            <select name="empresa_destino" id="form-field-1" class="col-xs-11 col-sm-6 col-md-8">
                                                <option value="" selected disabled="disabled" hidden>Selecione a Empresa</option>

                                                <?php while ($dados = mysql_fetch_array($busca_query)) { ?>

                                            <option value="<?php echo $dados['cod_empresa']; ?>"><?php echo $dados['razao_social']; ?></option> <?php } ?>

                                            </select>
                                        </div>
  • 1

    it is important that you learn to use the standard library PDO or mysqli. the functions mysql_* were discontinued in PHP 5.5 and in version 7.0 it ceased to exist. To ensure that your programs will work in updated versions of PHP, start migrating.

0

The solution you yourself have already talked about: using some structure of loop (while, foreach, for).

You didn’t say what executarSQL returns.

Assuming it returns a array, the code should look like this:

<select name="empresa_destino" id="form-field-1" class="col-xs-11 col-sm-6 col-md-8">
    <option value="">Selecione a Empresa</option>

    <?php foreach ($inserido as $codigo => $valor): ?>
    <option value="<?php echo $codigo ?>"><?php echo $valor ?></option>
    <?php endforeach; ?>
</select>

Consult the guide PHP the Right Way and the manual page to learn more about modern PHP.

Browser other questions tagged

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