1

I am doing paging with php for my search engine, so far everything is going well, but when I am trying to give the function to the previous button I recive an error related to the variable 'pagina', I am using the GET method and the buttons to change the page On page they work well for me. The data of my query is displayed correctly, everything works for me unless I check the error for the button to return to the previous page.

This is all my code.

<?php

     include_once 'conection.php';
    $sql = 'SELECT * FROM imagenesAn';
    $sentencia = $pdo->prepare($sql);
    $sentencia->execute();

    $resultado = $sentencia->fetchALL();

    $articulos_x_pagina = 10+1;
    //contar artículos de nuestra base de datos
    $total_articulos_db = $sentencia->rowCount();
    $paginas = $total_articulos_db / 10;
    $paginas = ceil($paginas);

    $salida = "";

    $query = "SELECT * FROM imagenesAn WHERE nombre NOT LIKE '' ORDER By id DESC LIMIT $articulos_x_pagina ";

    if (isset($_POST['consulta'])) {
        $q = $conn->real_escape_string($_POST['consulta']);
        $query = "SELECT * FROM imagenesAn WHERE Id LIKE '%$q%' OR nombre LIKE '%$q%' OR nombre_imagen OR registro LIKE '%$q%'";
    }


    if ($resultado AND $resultado->num_rows>0) {
        $salida.="<table class='table table-borderless tabla table-responsive-sm' style='width: 92%; margin-left: 100px; margin-top: 40px; text-align: center;'>
                <thead class='jeder'>
                    <tr class='text-primary'>
                    <th scope='col text-primary'>ID</th>
                    <th scope='col'>Nombre</th>
                </tr>
            </thead>
        <tbody class='cuerpo'>";
        //var_dump($resultado);
            foreach($resultado as $articulo): 
            $salida.="
                <tr>
                        <td style='text-align: center;'>".$articulo['id']."</td>
                        <td style='text-align: center;'>".$articulo['nombre']."</td>
                        <td style='text-align: center;'><a href='diagnostico?id=". $articulo['id']."'><i class='fas fa-file-medical-alt fa-2x' style='color: #c80000'></i></a></td>

                    </tr>";
            endforeach;?>

                <div class="contenedorPaginacion">
                    <nav aria-label="Paginación" class="paginacion">
                        <ul class="pagination">
                            <li class="page-item">
                                <a class="page-link" 
    /*This is the "76 line"*/  href="buscador.php?pagina=<?php echo $_GET['pagina']-1 ?>">
                                    Anterior
                                </a>
                            </li>

                            <?php for($i = 0;$i<$paginas;$i++){ ?>
                            <li class="page-item">
                                <a
                                href="buscador.php?pagina=<?php echo $i+1?>" class="page-link">
                                <?php echo $i+1?>
                            </a>
                            </li>
                            <?php }?>


                            <li class="page-item">
                                <a href="#" class="page-link">Siguiente</a>
                            </li>
                        </ul>
                    </nav>
                </div>


            <?php
        $salida.="</tbody></table>";
    }else{
        $salida.="<p class='parrafo' style='margin-left: 10px;'>No se encontraron resultados similares a su busqueda</p>";
    }


    echo $salida;

    $conn->close();

?>

I follow this youtube tutorial https://www.youtube.com/watch?v=d8odPbC8Yhk&t=481s

This is the error: ( ! ) Notice: Undefined index: pagina in C:\Proyecto\buscar.php on line 76 Call Stack #TimeMemoryFunctionLocation 10.0006422336{main}( )...\buscar.php:0 -1"> Anterior

Jesús
  • 448
  • 1
  • 3
  • 14
  • So let me get this right. When you have no variables to inject into your query, you use a prepared statement. Then when you actually _should_ be using a prepared statement with bound parameters, you don't use a prepared statement? – mickmackusa Jan 08 '20 at 04:52
  • 1
    Read the note in the documentation: https://www.php.net/manual/en/pdostatement.rowcount.php **PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.** If you are going to `fetchAll()` just call `count()` on that array. – mickmackusa Jan 08 '20 at 04:56
  • Is this a typo question? I see `$_POST['consulta']` earlier in the code. Do you mean to use `$_POST['pagina']` instead of `$_GET['pagina']`? – mickmackusa Jan 08 '20 at 05:02
  • $_POST['consulta'] is the value of a input type text, but it is working, my problem is in this line, href="buscador.php?pagina=", i know this couse when i delete it my code works, thanks for the recomendations – Jesús Jan 08 '20 at 05:10
  • So what do you see when you write `var_export($_POST);` and `var_export($_GET)`? – mickmackusa Jan 08 '20 at 05:14
  • I got this: "array ( )" in both. – Jesús Jan 08 '20 at 05:21
  • Do you have any redirects happening that may be "losing" this data for you? There is not enough detail in your question to determine the cause. The `$_GET` values will be plainly visible in the address bar. if you don't have `pagina=something` in the url, then you won't have `$_GET['pagina']`. – mickmackusa Jan 08 '20 at 05:23
  • This is my url: http://localhost/endocrino/buscador.php?pagina=2 – Jesús Jan 08 '20 at 05:31
  • 1
    Keep researching. Start here: https://stackoverflow.com/questions/3463642/get-is-empty-when-the-url-has-variables and keep going. – mickmackusa Jan 08 '20 at 05:38

0 Answers0