PDO does not return query results

-1

I have a code where you would have a select to select in which column you want to search and click on the letter to fetch within the selected column the selected letter.

The error is on my button. It would have to be <a href=""> only that by having a <form action=""> directed to another page href conflicts with form

Search code letter "A":

require_once 'functions.php';

    $PDO = db_connect();

    $opcao_filtro = filter_input(INPUT_POST, 'opcao_filtro', FILTER_SANITIZE_STRING);

    $sql = 'SELECT * FROM tablet AS t WHERE';

  //idlivro, titulo, tipo, e cor.
  switch($opcao_filtro)
  {
      switch($opcao_filtro)
{

    case 'titulo': {
        $sql .= ' t.titulo like "a%"';
        break;
    }
    case 'cor': {
        $sql .= ' t.cor like "a%"';
        break;
    }
    case 'categoria': {
        $sql .= ' t.categoria like "a%"';
        break;
    }
}


    $stmt = $PDO->prepare($sql);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $result = array_unique($rows);

        foreach($rows as $row)
        {
            echo $row['titulo'];
            echo $row['cor'];
            echo $row['categoria'];
        }

Button Tag:

<button type="submit" class="btn btn-link btn-xs" value="a" name="a">A</button>

Select:

<div class="op">
    <select name="opcao_filtro" class="form-control" style="width:150px; height:30px;">
        <option value="nulo">---</option>                
        <option value="titulo">Título</option>
        <option value="autor">Autor</option>
        <option value="tema">Tema</option>
        <option value="editora">Editora</option>                                    
    </select>
</div>
    
asked by anonymous 21.08.2018 / 13:33

1 answer

2

The assembly of your WHERE is incorrect, the ? (interrogation) sign will be replaced by the passed parameter, so WHERE should be set up as follows:

switch($opcao_filtro)
{
    case 'titulo': {
        $sql .= ' t.titulo like "?%"';
        break;
    }
    case 'autor': {
        $sql .= ' t.cor like "?%"';
        break;
    }
    case 'tema': {
        $sql .= ' t.categoria like "?%"';
        break;
    }
}
Another important point to note is that either you use = (equal) to compare integer values, or you use LIKE with % (percent) to compare partially.

    
21.08.2018 / 13:46