Rewrite the code to display expected result?

Asked

Viewed 121 times

0

This code must return all records from the table immovable if the array is empty and returns selected records if there are items in the array.

I can have the array mounted and the database consulted if I select at least one form item but cannot return all the values of the database if the array is empty. In fact it does not arrive empty, it arrives like this: [0] => and that’s the part that makes a mistake.

# Selecionando o tipo de imóvel
$tipo = $_POST['tipo'];
$tipo = (@explode('/', implode('/', $tipo)));

if (isset($tipo) && !empty($tipo)){

    // se $tipo for um array e for maior que 0
    if (is_array($tipo) && count($tipo) > 0) {
       $where .= " CATEGORIA IN ('".implode("','", $tipo)."') AND ";
    } else {
       $where .= " CATEGORIA = '{$tipo}' AND ";
    }
}

SQL without array items:

SELECT * FROM immovable WHERE 1=1 AND CATEGORY IN ('')

SQL with array items:

SELECT * FROM immovable WHERE 1=1 AND CATEGORY IN ('CASA')

Form

<input type="checkbox" name="tipo[]" value="CONJUNTOSALA/LOJA" id="tp5">
<label for="tp5">Conjunto/Sala/Loja</label>

<input type="checkbox" name="tipo[]" value="FLAT/LOFT" id="tp6">
<label for="tp6">Flat/Loft</label>

<input type="checkbox" name="tipo[]" value="PREDIO COMERCIAL" id="tp7">
<label for="tp7">Prédio Comercial</label>

<input type="checkbox" name="tipo[]" value="TERRENOS" id="tp8">
<label for="tp8">Terreno/Área</label>
  • The problem with this code is that even though I don’t select any item and send the form with array theoretically empty it commits SQL by adding AND CATEGORIA IN ('') because actually the array is not empty but has 1 empty index.

  • 2

    Mark avoid this kind of gambiarras of WHERE 1=1 and create your dynamically shaped querys. Also eliminate this explode with error suppression (@).

  • 1

    What is the purpose of the implode/explode line? php already transforms its input into array without having to do so ai: http://phpfiddle.org/main/code/n0kp-9hmf

  • It has indexes that arrive alone but it has indexes that arrive APARTAMENTO/APTO DUPLEX and I need to send these terms individually, break them up in the / (bar).

  • It wouldn’t be the case if you split your form into two fields? APARTAMENTO and APTO DUPLEX?

  • For reasons of layout it is no longer possible to implement this solution, it follows image: http://puu.sh/c2vtG/0348a1ad0f.png ... no space for more fields and they want to send 2 properties by input.

  • You say you always create the query with IN ('') even without informing any field?

  • In this case, how the query creates an empty index [0] => in the form Ubmit, the statement count($tipo) > 0 is TRUE and if it is TRUE he adds AND CATEGORIA IN ('') in SQL.

Show 3 more comments

1 answer

2


Your variable has an empty index because you are giving a explode. And there’s no need for that explode. So you can do it like this:

# Selecionando o tipo de imóvel
if (isset($_POST['tipo']) && is_array($_POST['tipo']) && count($_POST['tipo']) > 0){
   $where .= " CATEGORIA IN ('".implode("','", $_POST['tipo'])."') AND ";
}

According to your form, your variable $_POST['tipo'] will always be a array.

Note: It is not recommended to send data to the database without treatment. That is, search for security and variable handling before concatenating them with your Query SQL.

Browser other questions tagged

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