Search mysql database with php with multi-level

2

I am trying to perform the following query in the database, through an html form, but in php I am only able to fetch all the data from my table. In case the user would choose the type of filter and would be made a query in the database, I am using only one table in my database.

                               

     <form action="busca_filtro.php" method="GET">
         Data:
        <td align="left">
            <input name="tbxDe" type="text" value="13/05/2015" maxlength="10" id="tbxDe" style="height:23px;width:120px;">
            &nbsp;até
            <input name="tbxAte" type="text" value="14/05/2015" maxlength="10" id="tbxAte" style="height:23px;width:125px;">
            &nbsp;
        </td>
        <br><br>
         Tipo de Manifestação:
         <select name="tipoManifestacao" id="tipoManifestacao" style="">
            <option value="-1">Todos</option>
            <option value="0">Conservação de barreiras de concreto</option>
            <option value="1">Defensas metálicas e cercas</option>
            <option value="2">Mato alto ao longo da pista</option>
            <option value="3">Pichação ao longo da estrada</option>
            <option value="4">Limpeza de canaletas e bueiros</option>
            <option value="5">Iluminação de passarelas</option>
            <option value="6">Conservação de ponto de ônibus</option>
            <option value="7">Conservação de pontes, viadutos e túneis</option>
            <option value="8">Conservação de sinalização (placas, faixas)</option>
            <option value="9">Buraco no asfalto</option>
            <option value="10">Lixo na estrada</option>
            <option value="11">Queimada</option>
            <option value="12">Remoção de animais mortos da estrada.</option>
        </select>
        <br><br>
        Concessionária:
         <select name="concessionaria" id="concessionaria" style="">
            <option selected="selected" value="-1">Todas</option>
            <option value="Autovias">Autovias</option>
            <option value="CART">CART</option>
            <option value="CCR Autoban">CCR Autoban</option>
            <option value="CCR Rodoanel Oeste">CCR Rodoanel Oeste</option>
            <option value="CCR SPVIAS">CCR SPVIAS</option>
            <option value="CCR Via Oeste">CCR Via Oeste</option>
            <option value="Centrovias">Centrovias</option>
            <option value="Colinas">Colinas</option>
            <option value="Ecopistas">Ecopistas</option>
            <option value="Ecovias">Ecovias</option>
            <option value="Intervias">Intervias</option>
            <option value="Renovias">Renovias</option>
            <option value="Rodovias do Tiete">Rodovias do Tiete</option>
            <option value="Rota das Bandeiras">Rota das Bandeiras</option>
            <option value="Spmar">Spmar</option>
            <option value="Tebe">Tebe</option>
            <option value="Triangulo do Sol">Triângulo do Sol</option>
            <option value="Via Rondon">Via Rondon</option>
            <option value="vianorte">Via Norte</option>
        </select>
        <br><br>
        Status:
        <select name="status" id="status" style="">
            <option selected="selected" value="-1">Todos</option>
            <option value="0">Aguardando</option>
            <option value="1">Em Atendimento</option>
            <option value="2">Atendido</option>
        </select>
        <br><br>
        Prazo:
        <select name="dataAtendimento" id="dataAtendimento" style="">
            <option selected="selected" value="-1">Todos</option>
            <option value="0">Vencidas</option>
            <option value="1">Em Dia</option>
        </select>
        <br><br>
        <input type="reset" value="Remover">
        <input type="submit" value="Filtrar">
    </form> 

 </body>

<?php
//Conexão com o BD
include_once("Config.php");

if($tipoManifestacao = isset($_GET['tipoManifestacao"']) || 
$concessionaria = isset($_GET['concessionaria']) || 
$status = isset($_GET['status']) || 
$$dataAtendimento = isset($_GET['dataAtendimento'])){

    $sql = "SELECT * FROM tb_manifestacoes";
    $result=mysqli_query($db,$sql);


       while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            echo "Tipo Manifestacao :{$row['tipoManifestacao']}  <br> ".
                 "Concessionaria: {$row['concessionaria']} <br> ".
                 "Status: {$row['status']} <br> ".
                 "Data Atendimento : {$row['dataAtendimento']} <br> ".
                 "--------------------------------<br>";
        }
    }else{
        echo "Erro";
    }   
?> 
    
asked by anonymous 18.05.2015 / 17:48

2 answers

1

Try building the query. Like this:

<?php
$sql  = "SELECT ";
$sql .= isset($_GET['tipoManifestacao']) ? 'tipoManifestacao' :
        isset($_GET['concessionaria']) ? 'concessionaria' : 
        isset($_GET['status']) ? 'status' : 
        isset($_GET['dataAtendimento']) ? 'dataAtendimento' : '*';
$sql .= ' FROM tb_manifestacoes'

?>

From here the query data recovery follows.

The statement I put above works is a string of ifs and elses (only with other operators), selecting everything if no type is selected.

    
18.05.2015 / 21:58
0

I am assembling a check before to know the options that user chose following the start of the verification:

    if(!empty($tipoManifestacao AND $dataAtendimento AND $concessionaria AND $status AND $dataAtendimento))
        echo "Todos preenchidos";
    elseif(!empty($tipoManifestacao AND $dataAtendimento AND $concessionaria AND $dataAtendimento AND empty($status))){
        echo "Status Vazio";
    }
    elseif(!empty($tipoManifestacao AND $dataAtendimento AND $status AND $dataAtendimento AND empty($concessionaria))){
        echo "Concessionaria vazio";
    }
    elseif(!empty($dataAtendimento AND $status AND $dataAtendimento AND empty($tipoManifestacao))){
        echo "Tipo Manifestacao vazio";
    }
    elseif(!empty($tipoManifestacao AND $status AND $tipoManifestacao AND empty($dataAtendimento))){
        echo "Prazo vazio";
    }
    elseif(empty($tipoManifestacao AND $concessionaria AND $status AND $dataAtendimento)){
        echo "Todos vazios";
    }
    
19.05.2015 / 17:57