MySQL query problems [closed]

2

<form method="POST" class="w-100" action="pesquisa_diaria.php">
   
    <div class="card-body">
        <div class="row">
           
            <div class="col-sm-3"> 
                <label>Tipo de Venda</label>
                   <select class="custom-select" name="tipo_vendas" required>
                     <option value=''>Selecione...</option>
                     <option value='caixa'>Caixa</option>
                     <option value='delivery'>Delivery</option>
                     <option value='null'>Todos</option>
                 </select>
            </div>
            
            <div class="col-sm-3"> 
                <label>Tipo de Pagamento</label>
                   <select class="custom-select" name="tipo_pagamento" required>
                     <option value=''>Selecione...</option>
                     <option value='cartao_credito'>Cartão de Credito</option>
                     <option value='cartao_debito'>Cartão de Debito</option>
                     <option value='dinheiro'>Dinheiro</option>
                     <option value='null'>Todos</option>
                 </select>
            </div>
            
            <div class="col-sm-3"> 
                 <label>Data</label>
                 <input id="datepicker" name="data"  width="276" required/>
            </div>

            <div class="col-12 text-right align-self-end">
                </br><button type="submit" class="btn btn-success">Pesquisar</button>
            </div>
        </div>
    </div>
</form>

I have a form that does a query in the database, but I have a problem in the "All" options. When I select this option, the query does not yield any results.

SELECT * FROM vendas WHERE DATE(date_created) = '2018-12-12'
AND tipo_venda = 'null' AND tipo = 'null' ORDER BY 'id'
    
asked by anonymous 13.12.2018 / 00:49

1 answer

1

I think that when you speak todos you want it to select any value, if that's the case you need to do something like:

$WHERE = array();
if( $_GET['tipo_pagamento'] != "null" )  // so coloca se nao for null
    array_push($WHERE,"tipo='{$_GET['tipo_pagamento']}'");

if( $_GET['tipo_vendas'] != "null" ) // so coloca se nao for null
    array_push($WHERE,"tipo_venda ='{$_GET['tipo_pagamento']}'");

$WHERE = count($WHERE) ? "AND ".join($WHERE," AND ") : "";

$qry = "SELECT * FROM vendas WHERE DATE(date_created) = '2018-12-12' {$WHERE} ORDER BY 'id'"

print $qry;

The code above has not been tested, but that's the idea

    
13.12.2018 / 01:17