mysql search with only one of the selected fields

0

I have a question about how to organize this information, I have two selects in my code, that the user chooses the two, generates the table looking for who has the two information, but I would like the search to be done even if the user to select only one of the two fields

This is the request code

if($_SERVER['REQUEST_METHOD']=='POST'){
            $id_categoria = $_POST['id_categoria'];
            $id_sub_categoria = $_POST['id_sub_categoria'];
            $pagina = 1;
            $_SESSION['id_categoria'] = $id_categoria;
            $_SESSION['id_sub_categoria'] = $id_sub_categoria;

And in case this is query executed

                    $result_empresas = "SELECT * 
                                FROM clientes 
                                WHERE (categoria_id = '$id_categoria' OR '$id_categoria' IS NULL) AND 
                                ( subcategoria_id = '$id_sub_categoria' OR '$id_sub_categoria' IS NULL) AND
                                ('$id_sub_categoria' IS NOT NULL OR '$id_categoria' IS NOT NULL)
                                 LIMIT $inicio, $qnt_result_pg";

Thanks to all who can help.

    
asked by anonymous 14.03.2018 / 16:21

1 answer

0
$result_empresas = "SELECT * FROM clientes WHERE 
                   (categoria_id = '$id_categoria' OR '$id_categoria' = NULL) AND
                   (subcategoria_id = '$id_sub_categoria' OR '$id_sub_categoria' = NULL)                              
                   LIMIT $inicio, $qnt_result_pg"; 

I suppose this should only work, if it does not work with NULL , use '' .

$result_empresas = "SELECT * FROM clientes WHERE 
                   (categoria_id = '$id_categoria' OR '$id_categoria' = '') AND
                   (subcategoria_id = '$id_sub_categoria' OR '$id_sub_categoria' = '')                              
                   LIMIT $inicio, $qnt_result_pg"; 
    
14.03.2018 / 17:12