how to create search system using filter

Asked

Viewed 1,112 times

-1

I have a search system where I search users filtering by: name, age and email. If I filter by name and age and email, everything happens well, but if I filter only by age, for example, nothing is returned same try in the correct database. I think I’m doing it the wrong way, because I use the AND operator in the query, I mean, I want all three to return true, but that’s not really what I want. I want to filter by: age, or by age and email, or by age and email and name.

<?php 
$nome = addslashes($_GET['nome']);
$idade = addslashes($_GET['idade']);
$email = addslashes($_GET['email']);
    $sql = $pdo->query("SELECT * FROM usuarios WHERE nome = '$nome' AND idade = '$idade' AND email = '$email' ");

?>
  • You have to assemble querys to see if you receive null values in the parameters.

  • You have to query separately for each situation.

3 answers

0

I did not find errors in the code, for me it worked perfectly, what may be occurring is that your variable that receives the $_GET age is not receiving any value, in which case I recommend that you run the code below to check if the variables are receiving values.

<?php 
$nome = addslashes($_GET['nome']);
$idade = addslashes($_GET['idade']);
$email = addslashes($_GET['email']);

//verifica se todos os campos estao recebendo valores
if(!is_null($nome) && !is_null($idade) && !is_null($email)){
    echo "Todos os campos estão enviando valor";
}else{
    echo "Algum campo nao esta correto";
}
?>

Another tip I have for you, is to use the PDO this way to search data in the database, I realized is using PDO, so is in search of greater security for your code.

//select de dados de uma forma mais segura
$sql = $pdo->prepare("SELECT nome, idade, email FROM usuarios WHERE nome = :nome AND idade = :idade AND email = :email");
$sql->bindParam(':nome', $nome, PDO::PARAM_STR);
$sql->bindParam(':idade', $nome, PDO::PARAM_INT);
$sql->bindParam(':email', $email, PDO::PARAM_STR);
$sql->execute();
$linha = $sql->fetch(PDO::FETCH_ASSOC);
?>

Anyway, I hope I helped my friend.

  • thanks for the job in the answer but my problem remains the problem this when I search for a single example by age soon the others will be empty right and just there that this error because more as in my query has the operator AND for name and email it will return no results as empty are coming what I want when searching by age for example others are excluded from the query if I search by age and email then the name is excluded from the query

  • Get it, try using the operator OR instead of AND.

0

Here is an example of how to mount a conditional query according to the fields sent:

<?php
$where = array();
$nome = "";
$idade = "";
$email = "";
if(isset($_GET['nome'])) {
    if(($_GET['nome'] <> "") {
        $nome = $_GET['nome'];
        $where[] = 'nome LIKE :nome';
    }
}
if(isset($_GET['idade'])) {
    if($_GET['idade'] <> ""){
        $idade = $_GET['idade'];
        $where[] = 'idade LIKE :idade';
    }
}
if(isset($_GET['email'])) {
    if($_GET['email'] <> ""){
        $email = $_GET['email'];
        $where[] = 'email = :email';
    }
}

if(count($where)) {
    $sql = $pdo->prepare("SELECT nome, idade, email FROM usuarios WHERE ".implode(' AND ',$where));

    if($nome <> "") {
        $sql->bindParam(':nome', $nome, PDO::PARAM_STR);
    }   

    if($idade <> ""){
        $sql->bindParam(':idade', $idade, PDO::PARAM_INT);
    }   

    if($email <> ""){
        $sql->bindParam(':email', $email, PDO::PARAM_STR);
    }

    $sql->execute();

}
?>
  • 1

    You start a variable with an empty string and then only update its value if it is not an empty string. Basically the value will never be updated, your array $where will always be a array empty, independent of the entrance and thus the last if will never be executed. In other words, at the end of the day the code does nothing. I believe you were mistaken in the operators you used, could review the answer?

  • You are right, I was wrong to enter the answer, I have made the correction. Thank you for warning.

-1

Change your query to this

$sql = $pdo->query("SELECT * FROM usuarios WHERE nome LIKE '$nome' OR idade = '$idade' OR email LIKE '$email' ");

Browser other questions tagged

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