How do I search by NAME in PHP?

-1

I've tried it in many ways, but all of the problem in the query ...

Exemplo :
<form class="form-inline my-2 my-lg-0" method="post" action="home.php">
 <input class="form-control mr-sm-2" type="text" name="palavra" 
 placeholder="Pesquisar" aria-label="Search"> <button class="btn btn- 
 outline- 
 danger my-2 my-sm-0" type="submit">Pesquisar</button>
 </form>

  <?php 
  if(isset($_POST['palavra']) != '')
  {
    $palavra = $_POST['palavra'];
    $pesquisa = "where nome like '%palavra%' ";
  } 
  else
   {
    $pesquisa = '';
   }

    $querySelect = $link->query("select * from alunos $pesquisa");
    $linhas = mysqli_num_rows($querySelect);
    echo "Foram encontrados $linhas registros";
    echo '<br>';

   ?>
    
asked by anonymous 11.09.2018 / 16:19

2 answers

0

Try adjusting the following

1st - if condition:

if(isset($_POST['palavra']) != '')

To:

if(isset($_POST['palavra']))

2nd - Inside if:

$pesquisa = "where nome like '%palavra%' ";

To:

$pesquisa = "where nome like '%$palavra%' ";
    
11.09.2018 / 16:50
-1

Only use: isset($_POST['palavra']) and try changing query to:

select * from alunos .$pesquisa

If it does not work, please describe the error.

    
11.09.2018 / 16:26