Between with varchar field type

Asked

Viewed 132 times

0

I have two fields where an initial and final date is placed:

<form name="filtroClientes" action="consulta_contas.php" method="POST">

            <div class="form-group">
                <label for="nomefiltrar" style="color:#E4CDAC; font-size: 17px; font-family:Arial">Observação</label>
                <input type="text" class="teste" id="nomefiltrar" name="nomefiltrar" />
                <p>
                <label for="dataemissaoinicialfiltrar" style="color:#E4CDAC; font-size: 17px; font-family:Arial">Data Emissao Inicial</label>
                <input type="date" id="dataemissaoiniciofiltrar" class="teste" name="dataemissaoiniciofiltrar" />
                <p>
                <label for="dataemissaofiltrar" style="color:#E4CDAC; font-size: 17px; font-family:Arial">Data Emissao Final</label>
                <input type="date" class="teste" id="dataemissaofinalfiltrar" name="dataemissaofinalfiltrar" />             



                <input type="hidden" name="acao" value="filtrar"/>

            </div>


            <button type="submit" class="btn btn-custom btn-roxo">Filtrar</button>
    </div>
    </form>

Through a function I recover the value of these fields and perform a query to filter according to the values placed in this field: registra_php accounts.:

function filtraContas(){
    $contas = '';
    $banco = abrirBanco();
    if(isset($_POST['nomefiltrar'])){
        $nomefiltrar = $_POST['nomefiltrar'];
    }else{
        $nomefiltrar = '';
    }

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

    }else{
        $dataemissaoinicio = '';
    }

    if(isset($_POST['dataemissaofinalfiltrar'])){
        $dataemissaofinal = $_POST['dataemissaofinalfiltrar'];
    }else{
        $dataemissaofinal = '';
    }

    $sql = " SELECT c.*, c.dataemissao as dataemissaoformatada, c.datavencimento as datavencimentoformatada, p.nome, e.nome_especie FROM contas c INNER JOIN pessoa p ON (c.id_fornecedor = p.id) INNER JOIN especies e ON (c.id_especie = e.id) WHERE observacao LIKE '%$nomefiltrar%' OR c.dataemissao BETWEEN STR_TO_DATE('$dataemissaoinicio', '%d/%m/%Y') AND STR_TO_DATE('$dataemissaofinal ', '%d/%m/%Y') ";
    $resultado = $banco->query($sql);
    $banco->close();
    while($row = mysqli_fetch_array($resultado)){
        $contas[] = $row;
    }
    return $contas;
}

However, the between is not being applied. Why is this happening? The date field is a varchar type field in the bank. I realized that even if I change the field from "date" to "text" and remove STR_TO_DATE, the between is not yet realized, I believe it should work since in the bank it is like varchar and in this field would be taking the text format??

  • Your code is conceptually wrong, it is not possible to filter a date in a VARCHAR field. If it is really necessary, you should convert the database field to Date and not just the parameter being requested.

  • True. I’m converting everyone now, but I still can’t filter: $sql = " SELECT c.*, p.nome, e.nome_especie FROM contas c INNER JOIN pessoa p ON (c.id_fornecedor = p.id) INNER JOIN especies e ON (c.id_especie = e.id) WHERE observacao LIKE '%$nomefiltrar%' OR STR_TO_DATE(c.dataemissao, '%d/%m/%Y') BETWEEN STR_TO_DATE('$dataemissaoinicio', '%d/%m/%Y') AND STR_TO_DATE('$final date issuance', '%d/%m/%Y') ";

  • Run the following SELECT in your database and see if the two fields return the data in the same structure (typing): SELCT STR_TO_DATE(c.dataemissao, '%d/%m/%Y') as DatadoBanco, STR_TO_DATE('06/02/2018', '%d/%m/%Y') from contas c. One more thing, can’t you change the field type in the database? I mean delete the existing field and recreate it as Date?

  • The database returns correctly, I believe the problem is the c.note LIKE '%name% filter%', if you have nothing filled it will return all records, right? I tried to do an if: if(isset($_POST['filtername']){ $filtername = $_POST['filtername']; }Else{ $filtername = '9'; } , but still returns all results.

  • Try to capture the exact SQL your code is generating. And as a hint, test your SQL for parts... by adding filters one by one...

No answers

Browser other questions tagged

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