filter with 4 fields coming via GET in php?

1

I'm having trouble creating a filter with more than four fields in a normal example where I filter the user's gender so my Query is

<?php
try {
	$pdo = new PDO("mysql:dbname=projeto_filtrotabela;host=localhost", "root", "root");
} catch(PDOException $e) {
	echo "ERRO: ".$e->getMessage();
}
if(isset($_POST['sexo']) && $_POST['sexo'] != '') {
	$sexo = $_POST['sexo'];
	$sql = "SELECT * FROM usuarios WHERE sexo = :sexo";
	$sql = $pdo->prepare($sql);
	$sql->bindValue(":sexo", $sexo);
	$sql->execute();
} else {
	$sexo = '';
	$sql = "SELECT * FROM usuarios";
	$sql = $pdo->query($sql);
}
?>

As shown if my variable $ sex is set and is filled then it will filter with what I came by the variable otherwise shows the results without filter more how do I apply more than one filter in the query example I want filtered by sex and also by how do I do this more efficiently?

    
asked by anonymous 14.11.2017 / 00:54

1 answer

0

Use a compound condition.

  

The composite conditions are made up of several simple conditions bounded by operators AND or OR .

The AND operator indicates that both conditions must be true.

We use the OR operator when we want the final result to be true whenever at least one of the conditions is true.

$sql = "SELECT * FROM usuarios WHERE sexo=:sexo and idade=:idade";
    $sexo = $_POST['sexo']; 
    $idade = $_POST['idade'];   
    $sql = $pdo->prepare($sql);
    $sql->bindValue(":sexo", $sexo);
    $sql->bindValue(":idade", $idade);
    $sql->execute();
    
14.11.2017 / 02:12