doubts in the elaboration of code

Asked

Viewed 51 times

0

I was unable to get this code to return a message stating that it did not locate the requested data, I need to fix it and the other doubt is that even without putting anything in the search field if I press the send button it returns the first items of the database, I don’t know how to fix it.

<?php
$servidor = "-";
$usuario = "-";
$senha = "";
$database = "-";
//Criar a conexao
$conn = mysqli_connect($servidor, $usuario, $senha, $database);

$pesquisar = $_POST['pesquisa'];
$sql = "SELECT * FROM `jazigos` WHERE `NOME` LIKE '%{$pesquisar}%' LIMIT 1";
$resultado_cemiterio = mysqli_query ($conn,$sql);


 while($rows_sql = mysqli_fetch_array($resultado_cemiterio, MYSQLI_ASSOC)){

    echo "<b>Nome do Falecido</b>: ". $rows_sql["NOME"]  ."<br>";
    echo "<b>Data de Nascimento</b>: ". $rows_sql["DATA DE NASCIMENTO"]  ."<br>";
    echo "<b>Data de Falecimento</b>: ". $rows_sql["DATA DE FALECIMENTO"]  ."<br>";
    echo "<b>Quadra</b>: 0". $rows_sql["QUADRA"]  ."<br>";
    echo "<b>Jazigo</b>: ". $rows_sql["JAZIGO"]  ."<br>";
    echo "<b>Gaveta</b>: 0". $rows_sql["GAVETA"]  ."<hr>";

}
?>

2 answers

3


Use the mysqli_num_rows() to check the number of rows returned in the query mysql: http://php.net/manual/en/mysqli-result.num-rows.php

To not let the user submit an empty query and thus bring the first result, treat the POST and 'block' empty case.

<?php
$servidor = "-";
$usuario = "-";
$senha = "";
$database = "-";
//Criar a conexao
$conn = mysqli_connect($servidor, $usuario, $senha, $database);

$pesquisar = trim($_POST['pesquisa']);

if($pesquisar=='') die("DIGITE ALGO PARA EFETUAR A BUSCA");

$sql = "SELECT * FROM `jazigos` WHERE `NOME` LIKE '%{$pesquisar}%' LIMIT 1";
$resultado_cemiterio = mysqli_query ($conn,$sql);

$qtd = mysqli_num_rows($resultado_cemiterio));
if($qtd==0) die("NADA ENCONTRADO");

 while($rows_sql = mysqli_fetch_array($resultado_cemiterio, MYSQLI_ASSOC)){

    echo "<b>Nome do Falecido</b>: ". $rows_sql["NOME"]  ."<br>";
    echo "<b>Data de Nascimento</b>: ". $rows_sql["DATA DE NASCIMENTO"]  ."<br>";
    echo "<b>Data de Falecimento</b>: ". $rows_sql["DATA DE FALECIMENTO"]  ."<br>";
    echo "<b>Quadra</b>: 0". $rows_sql["QUADRA"]  ."<br>";
    echo "<b>Jazigo</b>: ". $rows_sql["JAZIGO"]  ."<br>";
    echo "<b>Gaveta</b>: 0". $rows_sql["GAVETA"]  ."<hr>";

}
?>
  • I’ll test that code

  • worked out this, and in the case of the boot that even with the blank search field, it shows the first items of the database

  • 1

    @Kadusilva edited the answer

  • our face worked super well, now I will see how to make pagination,because my database has 5 thousand item and as and name of people, several times appears the same name on the same page and gets very large

  • Behold datatables, is good! https://datatables.net/

  • 1

    And mark my answer as valid. :)

  • you know how to make pagination?

Show 2 more comments

1

To return the message that was not found something can follow the @Pedro Augusto tip using the mysqli_num_rows.

For the problem you have in the send button you could do a simple validation with an IF + Empty before executing your SELECT.

In your case it would look like this:

//...
//Criar a conexao
$conn = mysqli_connect($servidor, $usuario, $senha, $database);

$pesquisar = $_POST['pesquisa'];

if(empty($pesquisar)) die("Digite algo para pesquisar.");

$qtd = mysqli_num_rows($resultado_cemiterio));
if($qtd==0) die("NADA ENCONTRADO");

$sql = "SELECT * FROM `jazigos` WHERE `NOME` LIKE '%{$pesquisar}%' LIMIT 1";
//...
  • I’ll test Aki now to see

  • this die("Type something to search." ); gave error ) Parse error: syntax error, unexpected die (T_EXIT) on C: wamp64 www test search.php on line 13

  • One parenthesis was missing in the IF. @Pedro Augusto edited now, try again.

Browser other questions tagged

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