How do I do a NAME search filter in PHP ?

Asked

Viewed 572 times

-1

I have tried several 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>';

   ?>
  • 1

    Function isset returns a boolean, so it makes no sense to make isset($_POST['palavra']) != ''. See the documentation. Other than that, I recommend describing which "query errors" you got.

  • Ah... I am beginner in this, I saw in a tutorial that it worked for everyone... but not for me... the errors are : Notice: Undefined variable: link in C: xampp htdocs Horus home.php on line 34 'Fatal error: Uncaught Error: Call to a Member Function query() on null in C: xampp htdocs Horus home.php:34 Stack trace: #0 {main} thrown in C: xampp htdocs Horus home.php on line 34'

  • The variable $link, which connects to the bank, does not exist. Where?

  • define it in the connection.php : <?php $utf8 = header ("Content-Type: text/html; charset=utf-8"); $link = new mysqli('localhost', 'root', 'Horus'); $link->set_charset('utf8'); ; ?>

  • This file is not listed as included in your example. Could you describe your problem better? Sometimes omitting some parts of the code makes it impossible to answer the question.

  • Ah...I hadn’t really included the.php connection link on my home.php page (where you’re doing the search filter), now that it includes... it doesn’t show me any more errors, but it doesn’t do the search...

  • '%palavra%' should be '%{$palavra}%'

  • Anderson, thanks for trying to help me by putting '%{$word}%' gave error : Notice: Undefined variable: word in C: xampp htdocs Horus home.php on line 36

  • Impossible, you set the variable in the previous line. You changed something else in the code?

  • no! kkkk I haven’t changed at all...

  • Anderson, I mosqued, I’m sorry. Well it seems that we are on the right track, I made the change to '%{$palavra}%' now and when I search the name "Daniela" really returns me 1 record, but only shows the text : "1 records have been found" but it’s not just the Daniela record... it’s still showing all 120 students...

  • This is another problem, which was not addressed in this question. I recommend you open another by placing the code related to the listing.

  • thanks! at the moment I can not ask the question the site says "reached the limit of questions" kkkk....

Show 8 more comments

2 answers

0

Try to adjust the following

1º - if condition:

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

for:

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

2º - Inside the if:

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

for:

$pesquisa = "where nome like '%$palavra%' ";
  • Hi Mike... I made the changes and it still shows the error : Notice: Undefined variable: search in C: xampp htdocs Horus home.php on line 36

  • Mike, I made some changes to my code (the first one I posted) and changed '%word%' to '%{$word}%' ... is returning me the records now, but only in text format , example : now and when I search the name "Daniela" really returns me 1 record, but it only shows the text : "1 records have been found" but it does not only show the record of Daniela... still showing all 120 students..

  • so now it’s another very different problem, the query worked, only it’s not able to display

  • would have to see the code of how is being displayed this, or what is done after bringing the results

  • my home.php is like this at the beginning : <?php include_once 'banco_de_dados/conexao.php' ?>&#xA; <?php include_once 'includes/header.inc.php' ?>&#xA;<?php include_once 'includes/menu.inc.php' ?>&#xA;&#xA; #aqui começa o formulário&#xA; <hr class="pt-5">&#xA; <div class="row">&#xA; <div class="col-md-12">&#xA;&#xA;&#xA; <div class="container pt-5">&#xA; &#xA; <div class="card card-outline-secondary">&#xA; <div class="card-header">&#xA; <h3 class="mb-0">Alunos - Horus Crossfit</h3>

  • look, I believe that you should test the code to see if the query is working, if it is returning the records correctly, if it is I recommend asking another question focused on your current problem, because there is no way to continue answering the new error based on the previous error code

  • thanks! at the moment I can not ask the question the site says "reached the limit of questions" kkkk....

  • kkkkkk ok, I recommend joining Telegram groups, I participate in some PHP groups, there are plenty of beginners there, asking questions like yours, and lots of people helping

  • haha thanks Mike! I’ll check this sz

  • nothing ;* good luck

Show 5 more comments

-1

Use only: isset($_POST['palavra']) and try to change the query for:

select * from alunos .$pesquisa

If it doesn’t work describe the error obtained.

  • only using isset($_POST['word']) (line 31) { $word = $_POST['word']; $search = "Where name like '%word%' "; } Else { $search = '; } gave error : Parse error: syntax error, Unexpected '{' in C: xampp htdocs Horus home.php on line 31

Browser other questions tagged

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