Filter with select in php

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 you use only the first select filter works well, adding the 3 filter possibilities does not work anymore.

    
asked by anonymous 27.07.2018 / 15:42

1 answer

3

The name of the selects fields is the same, so the last one displayed on the screen and populated is what is stored in $ _POST, so it is not working. If you start your query you will notice that all three parameters have the same value.

Try using different names in the 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 vc 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'];
}
    
27.07.2018 / 16:02