Error using class to filter by student names

Asked

Viewed 31 times

-1

I’m trying to do a function to search by student’s name in the databank.

I’m having the following mistake:

Notice: Undefined index: filter in C: meuphp test search_student.php on line 7

   <?php 

    require 'conexao.php';
    include 'classe_aluno.php';

    $exibindo= new Aluno($mysql);
    $filtrado=$exibindo->exibirUm($_POST['filtro']);?>

The line showing the error is:

$filtrado=$exibindo->exibirUm($_POST['filtro']);?>

The second mistake is

Fatal error: Uncaught TypeError: Argument 1 passed to Aluno::exibirUm() must be of the type string, null given, called in C:\meuphp\teste\pesquisa_aluno.php on line 7 and defined in C:\meuphp\teste\classe_aluno.php:29 Stack trace: #0 C:\meuphp\teste\pesquisa_aluno.php(7)Student->displayUm(NULL) #1 {main} thrown

public function exibirUm(string $filtro){

        $exibir=$this->mysql->prepare("SELECT * FROM aluno WHERE nome LIKE  ?");
        $exibir->bind_param('s',$filtro);
        $exibir->execute();
        $filtrado=$exibir->get_result()->fetch_assoc();

        return $filtrado;
    }

1 answer

0


The problem is that when opening the page for the first time no POST was made, so $_POST['filtro'] does not exist. Consequently, the method exibirUm, that expects to receive a string, receives NULL.

To solve this problem, filter the database only when receiving the form data. Something like

if (isset($_POST['filtro']) === true) {
  $filtrado = $exibindo->exibirUm($_POST['filtro']);
}
  • It helped me a lot that thank you solved my problem. How can I be using this in the $filtered variable? I am passing result to a table of the html itself "table", <th><? php echo $filtered['name']; ? ></th>

  • @Medivh, I do not know your methods, but you can, before displaying the result, check if it is not a POST (or filtered is not set) and in this case do not display the table with result. In case it is a post, but no results are found, display a message. Finally, if it has resulted, displays the table.

Browser other questions tagged

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