When filter no record appears in the table

Asked

Viewed 24 times

0

When I filter in table no record appears. No error only no record appears.

I wanted to show you when to filter.

Could you help me?

Code:

<form method="POST">
Data Inicial <input name="datainicio" type = 'date'>
Data Final <input name="datafim"  type = 'date'>
<input type="submit" class="button3" value="Buscar">
</form>
<?php
if(isset($_POST['datainicio']) && isset($_POST['datafim'])){
    $datainicio=$_POST['datainicio'];
    $datafim=$_POST['datafim'];
    $query="SELECT * FROM custos WHERE (data>'$datainicio' AND data<'$datafim')";
    $res=mysqli_query($ligacao,$query) or die(mysqli_error($ligacao));
?>
<p></p>
<table style="width: 100%;" border='0' name="table">
   <thead>
        <tr>
        <th width="2%" align='center' bgcolor='#baba84'>Data</th>
        <th width="5%" align='center' bgcolor='#baba84'>Nome do custo</th>
        <th width="1%" align='center' bgcolor='#baba84'>Valor</th>
        </tr>
    </thead>
    <tbody>
<?php
$total_valor = 0;
while($registo = mysqli_fetch_assoc($res)){
    $valor=number_format($registo['valor'], 2, ',', ' ')."€";
    echo '<tr>';
    echo'<td align="center" bgcolor="white">'.$registo['data'].'</td>';
    echo'<td bgcolor="white">'.$registo['nome_custo'].'</td>';
    echo "<td bgcolor='white'>".$valor."</td>";
    print ("<td bgcolor='white' width='1%'><a href='alterar_custos.php?num={$registo['num']}&data={$registo['data']}&nome={$registo['nome_custo']}&valor={$registo['valor']}' class='button2'><i class='fa fa-edit'></i></</a></td>");
    print ("<td bgcolor='white' width='1%'><a href='eliminar_custos.php?num={$registo['num']}' class='button2'><i class='fa fa-trash'></i></</a></td>"); 
    echo'</tr>';
    $total_valor += $registo['valor'];
}
?>
</tbody>
<tfoot>
    <tr>
        <td align='left' colspan='2' bgcolor='#baba84'><b>Valor Total</b></td>
        <td align='left' bgcolor='white'><?php echo number_format($total_valor, 2, ',', ' ')."€";?></td>
    </tr>
</tfoot><!---->
</table>
<?php
mysqli_close($ligacao);
}
?>

1 answer

0

To search by date in the database, you need to enter the date in the aaa-mm-dd format, for example, if you type the date as "05/03/2020" the search will not work, you would have to enter the date as "2020-03-05".

Before searching, convert the date as follows:

$datainicio=date("Y-m-d", strtotime($_POST['datainicio']));
$datafim= date("Y-m-d", strtotime($_POST['datafim']));
$query="SELECT * FROM custos WHERE (data BETWEEN '$datainicio' AND '$datafim')";
$res=mysqli_query($ligacao,$query) or die(mysqli_error($ligacao));

Browser other questions tagged

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