Filter with select in php

Asked

Viewed 309 times

0

I have this code with the 3 selects to filter a query:

<form class="shadowbox6" action="" method="post">
    <strong><label for="Estado Encomenda">Estado Encomenda</label></strong>
        <select name="pesquisar"> 
            <option></option> 
            <?php 
                $sql = "SELECT * FROM centrodb.EstadoEncomendas"; 
                $qr = mysqli_query($conn, $sql); 
                while($ln = mysqli_fetch_assoc($qr)){ 
                    echo '<option value="'.$ln['Estado'].'"> '.$ln['Estado'].'</option>'; 
                } 
            ?> 
        </select>
        <strong><label for="Valência">Valência</label></strong>
        <select name="pesquisar"> 
            <option></option> 
            <?php 
                $sql = "SELECT * FROM centrodb.Destinos WHERE Id IN (1,2,3)"; 
                $qr = mysqli_query($conn, $sql); 
                while($ln = mysqli_fetch_assoc($qr)){ 
                    echo '<option value="'.$ln['Destino'].'"> '.$ln['Destino'].'</option>'; 
                } 
            ?> 
        </select>
        <strong><label for="Requerente">Requerente</label></strong>
        <select name="pesquisar"> 
            <option></option> 
            <?php 
                $sql = "SELECT * FROM centrodb.Requerentes WHERE Id IN (1,2,4,5,8)"; 
                $qr = mysqli_query($conn, $sql); 
                while($ln = mysqli_fetch_assoc($qr)){ 
                    echo '<option value="'.$ln['Requerente'].'"> '.$ln['Requerente'].'</option>'; 
                } 
            ?> 
        </select>
        <button class="botao" type="submit">Consultar</button>
</form>

if(isset($_POST['pesquisar']))){
    $pesquisar = $_POST['pesquisar'];

$result_cursos = "SELECT IdRequisicao, 
       CASE WHEN centrodb.EncomendasGerais.Estado IN (1,2,6) THEN DATE(DataEncomenda)
       WHEN centrodb.EncomendasGerais.Estado IN (3,4,5,7) THEN DataAprovacao END AS `Data`,
       IdTipoProduto,
       IdProduto,
       Quantidade,
       IdRequerente,
       IdDestino,
       Fornecedor,
       centrodb.EstadoEncomendas.Estado,
       Preco

FROM centrodb.EncomendasGerais LEFT OUTER JOIN centrodb.EstadoEncomendas

on centrodb.EstadoEncomendas.Id = centrodb.EncomendasGerais.Estado

WHERE centrodb.EstadoEncomendas.Estado LIKE '%$pesquisar%' AND IdDestino LIKE '%$pesquisar%' AND IdRequerente LIKE '%$pesquisar%'
...
}

If using only the first select works well the filter, adding the 3 filter possibilities no longer works.

  • Put the selects with different Ames, and at the time of the query, see if you received any posts from each select before adding it to the query.

1 answer

3


The name of the selected fields is the same, so the last one displayed on the screen and filled in is the one stored in $_POST["search"], so it’s not working. If you print your query you will notice that the three parameters have the same value.

Try to use different tables in selects and create three search variables, one for each select. Something like :

<select name="pesquisar_estado"> 
<select name="pesquisar_destino"> 
<select name="pesquisar_requerente"> 

and in php you get each select in a variable

if(isset($_POST['pesquisar_estado']))){
   $pesquisar_requerente = $_POST['pesquisar_requerente'];
}
if(isset($_POST['pesquisar_destino']))){
   $pesquisar_destino = $_POST['pesquisar_destino'];
}
if(isset($_POST['pesquisar_requerente']))){
   $pesquisar_requerente = $_POST['pesquisar_requerente'];
}

Browser other questions tagged

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