Method Search database records on the same PHP form page

Asked

Viewed 65 times

-1

Good morning guys, I am working on a personal project and I plan to make a page to search for customers informing their name and CPF for now the code is like this, but it is not working someone knows tell me why?

Here is the code below

Page code / part of form

            <form id="demo-form2" data-parsley-validate class="form-horizontal form-label-left" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">

                  <div class="form-group">
                    <label class="control-label col-md-1 col-sm-3 col-xs-12" for="first-name">Nome 
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                      <input type="text" id="first-name" name="nome" class="form-control col-md-7 col-xs-12">
                    </div>
                  </div>
                  <div class="form-group">
                        <label class="control-label col-md-1 col-sm-3 col-xs-12" for="last-name">CPF<span class="required">*</span>
                        </label>
                        <div class="col-md-3 col-sm-6 col-xs-12">
                          <input type="text" id="last-name" name="cpf" class="form-control col-md-3 col-xs-12">
                        </div>
                  </div>




                  <div class="ln_solid"></div>
                  <div class="form-group">
                    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                      <a href="index.html"><button class="btn btn-round btn-danger" type="button">Cancelar <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span></button></a>
                      <button class="btn btn-round btn-warning" type="reset">Resetar <span class="glyphicon glyphicon-repeat" aria-hidden="true"></span></button>
                      <button type="submit" class="btn btn-round btn-primary">Buscar <span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
                    </div>
                  </div>

                </form>

Part of listing the records

 <tbody>
                            <?php if(isset($_POST['submit'])): ?>
                            <?php foreach ($cliente->findClientes($nome,$cpf) as $key => $value): ?>
                              <tr class="even pointer">
                                <td class=" "><?php echo $value->id; ?></td>
                                <td class=" "><?php echo $value->nome; ?></td>
                                <td class=" "><?php echo $value->profissao; ?></td>
                                <td class="celula"><?php echo $value->endereco; ?></td>
                                <td class=" "><?php echo $value->numhab; ?></td>
                                <td class="telefone" style="width: 150px"><?php echo $value->telefone1; ?></td>
                                <td class="telefone" style="width: 150px"><?php echo $value->telefone2; ?></td>
                                <td class=" "><?php echo $value->bairro; ?></td>
                                <td class=" "><?php echo $value->cidade; ?></td>
                                <td class=" "><?php echo $value->uf; ?></td>
                                <td class=" "><?php echo $value->cep; ?></td>
                                <td class="cpf"><?php echo $value->cpf; ?></td>
                                <td class=" "><?php echo $value->email; ?></td>
                                <td class=" " style="text-align: center"><?php echo $value->usuario; ?></td>
                                <td class=" last" style="width: 100px"> <?php echo "<a href='condutores.php?id=" . $value->id . "'>Condutores <span class='glyphicon glyphicon-user' aria-hidden='true'></span></a> "; ?></td>
                                <td class=" last" style="width: 80px"> <?php echo "<a href='clientes.php?id=" . $value->id . "'>Editar <span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></a> "; ?></td>
                                <td class=" last" style="width: 80px"> <?php echo "<a class='delete' href='../Controllers/deletarCliente.php?&id=" . $value->id . "' data-confirm-cliente='Deseja excluir este cliente? '>Excluir <span class='glyphicon glyphicon-remove' aria-hidden='true'></span></a> "; ?></td>
                              </tr>
                              <?php endforeach; ?>

                              <?php endif; ?>

                            <?php foreach ($cliente->findAll() as $key => $value): ?>
                              <tr class="even pointer">
                                <td class=" "><?php echo $value->id; ?></td>
                                <td class=" "><?php echo $value->nome; ?></td>
                                <td class=" "><?php echo $value->profissao; ?></td>
                                <td class="celula"><?php echo $value->endereco; ?></td>
                                <td class=" "><?php echo $value->numhab; ?></td>
                                <td class="telefone" style="width: 150px"><?php echo $value->telefone1; ?></td>
                                <td class="telefone" style="width: 150px"><?php echo $value->telefone2; ?></td>
                                <td class=" "><?php echo $value->bairro; ?></td>
                                <td class=" "><?php echo $value->cidade; ?></td>
                                <td class=" "><?php echo $value->uf; ?></td>
                                <td class=" "><?php echo $value->cep; ?></td>
                                <td class="cpf"><?php echo $value->cpf; ?></td>
                                <td class=" "><?php echo $value->email; ?></td>
                                <td class=" " style="text-align: center"><?php echo $value->usuario; ?></td>
                                <td class=" last" style="width: 100px"> <?php echo "<a href='condutores.php?id=" . $value->id . "'>Condutores <span class='glyphicon glyphicon-user' aria-hidden='true'></span></a> "; ?></td>
                                <td class=" last" style="width: 80px"> <?php echo "<a href='clientes.php?acao=editar&id=" . $value->id . "'>Editar <span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></a> "; ?></td>
                                <td class=" last" style="width: 80px"> <?php echo "<a class='delete' href='../Controllers/deletarCliente.php?&id=" . $value->id . "' data-confirm-cliente='Deseja excluir este cliente? '>Excluir <span class='glyphicon glyphicon-remove' aria-hidden='true'></span></a> "; ?></td>
                              </tr>

                              <?php endforeach; ?>
                            </tbody>

Function code findClients

 public function findClientes($nome,$cpf){

    $conexao = new Conexao();

    $sql = "SELECT * FROM $this->table WHERE nome LIKE '%:nome%' AND cpf LIKE '%:cpf%' ";

    return $conexao->select($sql, array(":nome" => $nome, ":cpf" => $cpf));
}

Thanks for your help

  • Is returning some error ?

  • Could post the error. But one thing I noticed in a quick look is the query part. You should add the joker % variable, and not query: nome LIKE '%:nome%' would be nome LIKE :nome, then you add the joker in the variable before "bindar": $nome = '%'.$nome.'%';

  • ok Noobsaibot I will send the error and check this question of the query

  • I tried and it didn’t work

  • The error is that the search does not occur

1 answer

1

No need to declare the function with the public, just do so:

function findClientes($nome,$cpf){ 
...

}
  • I didn’t understand it very well but it still doesn’t work

  • I thought it was simple to do but I’m trying to make a get method still won’t

  • The values are arriving correctly in the query?

  • Yes, I managed to resolve thank you

Browser other questions tagged

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