I have two fields where a start and end date is set:
<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 retrieve the value of these fields and perform a query to filter according to the values placed in this field: registra_contas.php:
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 entre is not being applied. Why does this occur? The date field is a field of type varchar in the database. I noticed that even if I change the field from "date" to "text" and remove the STR_TO_DATE, the entre is still not performed, I believe it should work since the bank is type varchar and in that field would be catching in text ?? format