Build in PHP a search system for period

Asked

Viewed 39 times

-2

I created a form with the aim of searching the person’s name and with the start date and end date field, with the intention of taking from the bank the amount of service within a month for this person searched. I was able to assemble to search the total, using SUM in the field, but I can’t find a way to associate the search by date, follow the code I’m trying to assemble:

Table inserir a descrição da imagem aqui

first file

<form method="POST" action="http://127.0.0.1/AppAtendimentoFisioterapia/php/sistema/relatorio.php">
    Fisioterapeuta:<input type="text" name="buscar" size="50"placeholder="Informe o nome do(a) fisioterapeuta"><br><br>


<label for="exampleInputPassword1" class="form-label">Período Inicial</label>
         <input name="dtAtendimentoinicial" type="date" class="form-control" placeholder="Date" aria-label="State">
  </div>

  <div class="col-sm mb-3">
         <label for="exampleInputPassword1" class="form-label">Período Final</label>
         <input name="dtAtendimentofinal" type="date" class="form-control" placeholder="Date" aria-label="State"><br><br>


         <input type="submit" value="Pesquisar">
</form>

2nd file:

<?php 

            
            $buscar = $_POST['buscar'];
            $dtInicial = date('Y/m/d', strtotime($_POST['dtAtendimentoinicial']));
            $dtFinal = date('Y/m/d', strtotime($_POST['dtAtendimentofinal']));

            $sql = mysqli_query($conn, "SELECT nmfisioterapeura, SUM(vlreceber), date_format(dtatendimento, '%d/%m/%Y') AS dtatendimento FROM registroatendimento WHERE nmfisioterapeura Like '%'.$buscar.'%' AND BETWEEN '$dtInicial' AND '$dtFinal'");

          
            $row = mysqli_num_rows($sql);
            if ($row > 0) {
              while ($linha = mysqli_fetch_array($sql)) {
                  $nomeFisio = $linha['nmfisioterapeura'];
                  $valorReceber = $linha['SUM(vlreceber)'];
                  $dtI = $linha['dtatendimento'];
                  $dtF = $linha['dtatendimento'];
                  
                }

              }
                    ?>

                    <tr style="border:1px solid #f2f2f2;">    
                      <td align="center" style="border:1px solid #f2f2f2;"><?php echo $nomeFisio ?></td>
                      <td align="left" style="border:1px solid #f2f2f2;"><?php echo $valorReceber ?></td>
                      <td align="center" style="border:1px solid #f2f2f2;"><?php echo $dtInicial ?></td>
                      <td align="center" style="border:1px solid #f2f2f2;"><?php echo $dtFinal ?></td>
                    </tr>

1 answer

-1

Try the following code instruction

 $sql = mysqli_query($conn, "SELECT nmfisioterapeura, SUM(vlreceber), date_format(dtatendimento, '%d/%m/%Y') AS dtatendimento FROM registroatendimento WHERE nmfisioterapeura Like '%'.$buscar.'%' AND BETWEEN '$dtInicial' AND '$dtFinal' GROUP BY dtatendimento");

Clause documentation GROUP BY in Mysql https://dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html

  • I changed, but still error: Fatal error: Uncaught Typeerror: mysqli_num_rows(): Argument #1 ($result) must be of type mysqli_result, bool Given in C: xampp htdocs18 Stack trace: #0 C: xampp htdocs

  • mysqli_query returns false in case of failure https://www.php.net/manual/en/mysqli.query.php . I recommend debugging the code, take the value passed in the second parameter of the function mysqli_query and run phpMyAdmin to find out where the error is in the query. It could be one of the variables that in the concatenation had no value. I strongly recommend Prepared Statements https://www.php.net/manual/mysqli.quickstart.prepared-statements.php

Browser other questions tagged

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