Problem with PHP Search Engine

Asked

Viewed 76 times

-3

GOOD AFTERNOON GUYS

I am developing a website for a client and he wants to put a locator (search), in case put the name of the person and appear her burial place.

only that I made the php code and the database, but when I do the test, appears the blank result.

appears like this to me:

Cemiterio Municipal Parque Águas Lindas
Localizador de Jazigos
Resultado da Pesquisa:
Resultado da Pesquisa:
Resultado da Pesquisa:
Resultado da Pesquisa:
Resultado da Pesquisa:

and in case it was to appear like this:

Cemiterio Municipal Parque Águas Lindas
Localizador de Jazigos
Resultado da Pesquisa:maria claudia
Resultado da Pesquisa:maria cunha
Resultado da Pesquisa:maria lina
Resultado da Pesquisa:maria da luz
Resultado da Pesquisa:maria costa

the form code and the one I made:

<h1><center>Cemiterio Municipal Parque Águas Lindas</center></h1>
<h1><center>Localizador de Jazigos</center></h1>

<form method="POST" action="pesquisar.php">
    <center> Pesquisar:<input type="text" name="pesquisar" placeholder="Nome do Falecido">
    <input type="submit" value="ENVIAR"></center>
</form>

and the search code and this:

<h1><center>Cemiterio Municipal Parque Águas Lindas</center></h1>
<h1><center>Localizador de Jazigos</center></h1>
<?php
    $servidor = "localhost";
    $usuario = "root";
    $senha = "EU NAO POSTEI A SENHA";
    $database = "cemiterio";
    //Criar a conexao
    $conn = mysqli_connect($servidor, $usuario, $senha, $database);

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

    while($rows_sql = mysqli_fetch_array($resultado_cemiterio)){
        echo "Resultado da Pesquisa:" ."<br>";
    }
?>

I REALLY NEED HELP WITH THIS PROJECT

And taking advantage, I need the result to present all the information of the row of the searched name ( the table has 8 columns) type search "maria lima" there appears so:

mome :maria lina Data de nascimento: 10/10/190  idade: 80 anos 
 Data de falecimento: 01/10/2018 Quadra: 03 Jazigo: 1250 Gaveta: 03 

I don’t know what else to do, I did a lot of research, but I couldn’t find a solution

thanks for your attention and collaboration.

  • Put the code on so we can look, or it’ll be impossible to help you.

  • You just want to do a simple search for a text typed in the form.

  • 1

    'echo "Search Result:" ." <br>";' you have a logic problem here, there should be something like: echo "Resultado da Pesquisa:" . $rows_sql['col1'] . "<br>"

2 answers

-1

In case you are really managing to do the search and it is returning you the results, what is missing is you display them. Inside the loop while replace with this:

 while($rows_sql = mysqli_fetch_array($resultado_cemiterio, MYSQLI_ASSOC)){
        echo "Resultado da Pesquisa:". $rows_sql["nome_da_coluna_da_sua_tabela"]  ."<br>";
    }

Trade the query for this:

 $sql = "SELECT * FROM `jazigos` WHERE `NOME` LIKE '%{$pesquisar}%' LIMIT 5";

You are forgetting to display the result through the indexes of your table on array returned

  • man, it worked out

  • Can I take one more doubt?

  • @Kadusilva Yes, it is not.

  • this my table it has 8 columns like this: name -birth date-age-death date- square-grave-drawer the client wants that when I put the name of the person shows the results of the line

  • because now only shows the name of the person

  • @Kadusilva for you to be able to list the 8 columns just you in the bottom row, do the same procedure but only by changing the name of the columns, for example:echo "<br/>".$rows_sql["coluna_x"]." <br/>".$rows_sql["coluna_y"] and so on.

  • ai shows the data of the person’s name line

  • more at what point I put it?

  • @Kadusilva just add in the same line, for example: echo "Resultado da Pesquisa:".$rows_sql["coluna_x"]."<br/>".$rows_sql["coluna_y"]."<br/>".$rows_sql["coluna_z"];

  • I will test excuse the delay, get out of the cemetery, I came home just now and now that I came to the office

  • in case it would be so:

  • <?php $server = "localhost"; $user = "root"; $password = "201620682"; $database = "cemitery"; //Create connection $Conn = mysqli_connect($server, $user, $password, $database); $search = $_POST['search']; $sql = "SELECT * FROM jazigos WHERE NOME LIKE '%{$search}%' LIMIT 25"; $resultado_cemiterio = mysqli_query ($Conn,$sql); while($rows_sql = mysqli_fetch_array($resultado_cemiterio, MYSQLI_ASSOC)){ echo "Search Result:". $rows_sql["NAME"] ." <br>"; echo "Search Result:". $rows_sql["DATA_DE_NASCIMENTO"] ." <br>"; } ?>

Show 7 more comments

-1

Your result is blank because you are not writing the data on echo

while ($rows_sql = mysqli_fetch_array($resultado_cemiterio)) {
    //Aqui você escrever apenas um HTML fixo, não tem os dados do banco
    echo "Resultado da Pesquisa:" ."<br>";
}

What you want is something like this:

echo "Resultado da Pesquisa:<br>";
while ($rows_sql = mysqli_fetch_array($resultado_cemiterio)) {
    echo implode(", ", $rows_sql)."<br>";
}

In this simple example I just joined the result columns with a comma but you can arrange as you prefer by picking each column separately through the indexes, from a look at the documentation of mysqli_fetch_array

Observing:

  • The way your code is is vulnerable to SQL Injection
  • If a variable is not sent pesquisar by the body of the request, the code will break, it would be necessary to use something like $pesquisar = isset($_POST['pesquisar']) ? $_POST['pesquisar']; : '' or $pesquisar = $_POST['pesquisar']; ?? '' (PHP 7)

Browser other questions tagged

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