Error 1065 Query is Empty when changing page in my pagination

Asked

Viewed 24 times

1

When I enter the first page loads the table normally but when I go to the second gives this error

<?php
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ERROR | E_WARNING | E_PARSE);
?>
<!DOCTYPE HTML>
<html lang="pt-br">
<head>
    <link rel="stylesheet" type="text/css" href="css.css">
    <meta charset="utf-8">
    <title>TCC Consulta</title>
</head>
<body>
    <h1><a href="index.php">Controle de TCC</a></h1>

    <div >
    <?php
    require_once "conexao/conexao.php";
    $maximo = 3;

    if (isset($_REQUEST['filtro'])) {
        $filtro = $_REQUEST['filtro'];
     }

    $pagina = intval(($_GET["pagina"]));
    if($pagina == "") {
        $pagina = "1";
    }


    $inicio = $pagina - 1;
    $inicio = $maximo * $inicio;

    $opcao = $_POST['opcao'];

        if($opcao=="opcProtocolo")
            $ComandoSQL = "select * from tb_tcc where num_protocolo like '$filtro%'";
        if($opcao=="opcOrientador")
            $ComandoSQL = "select * from tb_tcc where nm_orientador like '$filtro%'";
        if($opcao=="opcMembroBanca"){
            $ComandoSQL = "select * from tb_tcc where membro_banca1 like '$filtro%' or membro_banca2 like '$filtro%' or membro_banca3 like '$filtro%'
            or membro_banca4 like '$filtro%' or membro_banca5 like '$filtro%' or membro_banca6 like '$filtro%'";
        }

    $result = $conn->query($ComandoSQL);
    $rows = $result->fetchAll();
    $total = count($rows);

    if("$opcao"=='opcProtocolo')
        $ComandoSQL = "select * from tb_tcc where num_protocolo like '$filtro%' LIMIT $inicio, $maximo";

    if("$opcao"=='opcOrientador')
        $ComandoSQL = "select * from tb_tcc where nm_orientador like '$filtro%' LIMIT $inicio, $maximo";

    if("$opcao"=='opcMembroBanca'){
        $ComandoSQL = "select * from tb_tcc where membro_banca1 like '$filtro%' or membro_banca2 like '$filtro%' or membro_banca3 like '$filtro%'
         or membro_banca4 like '$filtro%' or membro_banca5 like '$filtro%' or membro_banca6 like '$filtro%' LIMIT $inicio, $maximo";
    }

    $result = $conn->query($ComandoSQL);
        try{
            if($total>0)
            {
                ?>
                <table>
                    <tr>

                        <td><b><font size="2" face="verdana">N&ordm; de<br/> protocolo&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">Autor&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">Orientador&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">Co-Orientador&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">Banca&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">T&iacute;tulo do trabalho&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">Data da proposta</b></font></td>
                        <td><b><font size="2" face="verdana">Data da defesa&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">&nbsp;&nbsp;Resumo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">Palavras chave&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana"> &Aacute;rea&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">TCC&nbsp;&nbsp;</b></font></td>
                        <td><b><font size="2" face="verdana">Opera&ccedil;&atilde;o&nbsp;&nbsp;</b></font></td>
                    </tr>
                    <tr><td colspan='26' height='5' bgcolor='#66CDAA'></td></tr><br/>

                <?php
                while ($row = $result->fetch(PDO::FETCH_OBJ))
                {
                    ?>
                        <tr>
                            <td><?php echo $row->num_protocolo; ?>&nbsp;</td>
                            <td><?php echo $row->nm_autor; ?>&nbsp;</td>
                            <td><?php echo $row->titulo.'. '.$row->nm_orientador; ?>&nbsp;</td>
                            <td><?php echo $row->tituloc.'. '.$row->nm_coorientador; ?>&nbsp;</td>
                            <td><?php echo $row->ttmb1.'. '.$row->membro_banca1.", ".$row->ttmb2.'. '.$row->membro_banca2.", ".$row->ttmb3.'. '.$row->membro_banca3;if($row->membro_banca4!="")echo ", ".$row->ttmb4.'. '.$row->membro_banca4;if($row->membro_banca5!="")echo ", ".$row->ttmb5.'. '.$row->membro_banca5;if($row->membro_banca6!="")echo ", ".$row->ttmb6.'. '.$row->membro_banca6; ?>&nbsp;</td>
                            <td><?php echo $row->titulo_trabalho; ?>&nbsp;</td>
                            <td><?php echo $row->dt_proposta; ?>&nbsp;</td>
                            <td><?php echo $row->dt_defesa; ?>&nbsp;</td>
                            <td><?php echo $row->resumo; ?>&nbsp;</td>
                            <td><?php echo $row->palavras_chaves; ?>&nbsp;</td>
                            <td><?php echo $row->area; ?>&nbsp;</td>
                            <td><?php echo $row->tcc; ?>&nbsp;</td>
                            <td>
                            <?php
                            echo "<a href='opcao.php?id=".$row->id . "'>";
                            echo "<img src='imagens/b_edit.png' border='0'><img src='imagens/b_drop.png' border='0'></a>&nbsp;\n";
                            ?>
                            </td>
                        </tr>
                    <?php
                }
                $conn = null;
                ?>
                </table>
                <?php
            }
            else
            {
                echo "<script>alert('Não há cadastros!');window.location='index.php'</script>";
            }
        }
        catch(PDOException $e)
        {
            echo "Erro!: ".$e."\n";
        }
    $menos = $pagina - 1;
    $mais = $pagina + 1;
    $pgs = ceil($total / $maximo);
     if($pgs > 1 ) {
        echo "<br clear='all'/><br /><br />";

      if($menos > 0) {
       echo "<a href='relatorio_paginacao_filtro.php?pagina=$menos&filtro=$filtro'>anterior</a>&nbsp; ";
      }


   for($i=1;$i <= $pgs;$i++) {
       if($i != $pagina) {
           echo "<a href='relatorio_paginacao_filtro.php?pagina=$i&filtro=$filtro'>$i</a> | ";
       } else {
           echo " <strong><font color='#000'>$i</font></strong> | ";
       }
   }

   if($mais <= $pgs) {
       echo "<a href='relatorio_paginacao_filtro.php?pagina=$mais&filtro=$filtro'>Pr&oacute;xima</a>";
   }
}


    ?>  
    </div>

</body>
</html>

1 answer

0

This is where you take the option you selected to do the initial search that will provide the data to the page and thus calculate the pagination need, correct?

$opcao = $_POST['opcao'];

if($opcao=="opcProtocolo")
    $ComandoSQL = "select * from tb_tcc where num_protocolo like '$filtro%'";
if($opcao=="opcOrientador")
    $ComandoSQL = "select * from tb_tcc where nm_orientador like '$filtro%'";
if($opcao=="opcMembroBanca"){
    $ComandoSQL = "select * from tb_tcc where membro_banca1 like '$filtro%' or membro_banca2 like '$filtro%' or membro_banca3 like '$filtro%' or membro_banca4 like '$filtro%' or membro_banca5 like '$filtro%' or membro_banca6 like '$filtro%'";
}

And here is the exposure of the links to the other pages, if any:

$menos = $pagina - 1;
$mais = $pagina + 1;
$pgs = ceil($total / $maximo);
if($pgs > 1 ) {
    echo "<br clear='all'/><br /><br />";
}
if($menos > 0) {
    echo "<a href='relatorio_paginacao_filtro.php?pagina=$menos&filtro=$filtro'>anterior</a>&nbsp; ";
}
for($i=1;$i <= $pgs;$i++) {
    if($i != $pagina) {
        echo "<a href='relatorio_paginacao_filtro.php?pagina=$i&filtro=$filtro'>$i</a> | ";
    } else {
        echo " <strong><font color='#000'>$i</font></strong> | ";
    }
}
if($mais <= $pgs) {
    echo "<a href='relatorio_paginacao_filtro.php?pagina=$mais&filtro=$filtro'>Pr&oacute;xima</a>";
}

The big question is that on the first page, on which you are loading correctly, you have the $_POST["opcao"] which, in the second part where paging is done, is not passed as a parameter for the next pages and without this parameter, which is responsible for defining their querys the system accuses the error that it is empty.

I suggest you make a brief change to add the field $_POST["opcao"] the parameters sent to the next pages, for example:

if(isset($_POST['opcao'])){
    $opcao = $_POST['opcao'];
}elseif(isset($_GET['opcao'])){
    $opcao = $_GET['opcao'];
}else{
    $opcao = "opcProtocolo"; //coloquei esse else como opção para vc entender que pode manter um valor padrão caso ocorra algum problema
}

And in the pagination links would look like this:

echo "<a href='relatorio_paginacao_filtro.php?opcao=$opcao&pagina=$menos&filtro=$filtro'>anterior</a>&nbsp; ";

Note that I added the opcao=$opcao right in the first parameter. I hope I helped.

Browser other questions tagged

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