Correct where clause with like null operator that finds data with nulls parameters

0

I have a query that is taking the null fields with the like operator. Let's look at an example in the query below:

$palavraChave = null;
$consulta = "select * from receitas where receita like '%$palavraChave%' ";  

Note that if I set the variable $ wordChave to null, it still takes all the data from the database, even ignoring the where clause. I think an error should not include the null condition.

Here comes the question: why are you doing this? Because I have two fields and one of the two can be empty.

I want to search by date and by keyword. If the date is null, the only search parameter will be the keyword. If it is otherwise, only the date will serve as a search condition. And in the error that I am referring to, is taking the data regarding the field filled in as to the null field, that is, it brings the two conditions, regardless of null or not. In short, I just want what I ask.

Here is a slightly different code with some changes:

$ _ POST ['data'] = '2015-11-15'; $ _POST ['word'] = 'inss';

$ condition="WHERE req_id="; ";

if ($ _ POST ['data']) {     $ condition.="AND data = '". $ _ POST [' data ']. "'";
}

if ($ _ POST ['word']) {
    $ condition.="AND LIKE word = '" $ _ POST [' word ']. "'";
}

$ query="SELECT * FROM recipes {$ condition}";

$ query = mysql_fetch_array ($ query, $ server);

? >

In this change, you can not find the first argument (the $ query)

    
asked by anonymous 07.11.2015 / 14:31

1 answer

0

You can elaborate as follows:

$condicao = "WHERE id != '' ";

if($_POST['data']){
    $condicao .= " AND data = '".$_POST['data']."'";
}

if($_POST['palavra']){
    $condicao .= " AND LIKE palavra = '".$_POST['palavra']."'";
}

$consulta = "SELECT * FROM receitas {$condicao}";
    
07.11.2015 / 14:37