1

I have a search form that searches a mysql database and return results. I also have a searchmodel.php file that is supposed to paginate results.However, the pagination does not return results for page 2 and other pages. The search form looks like this:

<?php

?>
<html>
<form action = "searchmodel.php" method = "GET">
            <select name = "choice">
                <!--<option value = "<?php echo $_GET['title']; ?>" name = "title">title</option>-->
                <option value = "title" name = "title">title</option>
                <option value = "author" name = "author"> author</option>
                <option value = "scc" name = "scc"> SCC_NO</option>
            </select>
                <input name= "term" type ="text" size="65" maxlength = "88" style = "display:inline">
                <input name = "mysearch" type="submit"  style = "display:inline">

        </form>

</html>

The searchmodel.php file looks like this:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
error_reporting(E_ERROR | E_PARSE);
include "data.php";
$con = dbConnect();
$perpage = 20;
echo ($pagenumber = isset($_GET['page']) ? $_GET['page'] : 1) . '<br />';

$offset = ($pagenumber-1) * $perpage;
$term = isset($_GET['term']) ? $_GET['term'] : null;
$choice = isset($_GET['choice']) ? $_GET['choice'] : '';
            if(empty($term) && ($choice == 'title' || $choice == 'author')){
                header ('Location: ' . 'search.php');   
                }

                if($choice == 'title') {
            $sth = $con->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM manuscript WHERE title 
            LIKE '%$term%' LIMIT {$offset},{$perpage}");
            $sth->execute();
            //$results = $sth->rowCount();
            //echo '<b>' . $results . '</b>'. '<br />';
            $results = $sth->fetchAll();
            $x=1;


            foreach($results as $key => $value){
                echo $x++ . ':' .$value['title'] . '<br />';


            }   
        }

            $total = $con->query("SELECT FOUND_ROWS() as total")->fetch()['total'];
            var_dump($total);
            $pages = ceil($total/$perpage);
            echo $pages . '<br />';



        for ($x=1; $x<=$pages; $x++){
        //echo $x;

        echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
    //echo "<a href='?pagenumber=$x'>$x</a>";

    }

I have searched this site(stackoverflow) for a question similar to mine and have found these two. This one and this one. However, the answers provided seem not to work for my case. My question is, what might i be doing wrong?? Is it the $_GET['choice'] variable causing the problem?Thanking you in advance

Alex Maina
  • 296
  • 3
  • 11
  • You are misusing prepared statements and are open to SQL injections. On page 2 etc. what does the SQL come out as? – chris85 Apr 15 '18 at 13:33
  • Thanks @chris85. The injection is not a major worry at the moment. Rather, the main worry is how to get pagination work – Alex Maina Apr 15 '18 at 14:05
  • It could cause issues with your query though; which might be your current issue. Please address the second part of my comment `On page 2 etc. what does the SQL come out as?`. – chris85 Apr 15 '18 at 14:21
  • On `page 2 etc` i get a blank page.Actually, if i click on any pagination link including `page 1` i get a blank page – Alex Maina Apr 15 '18 at 14:27
  • Is it erring? e.g. you get a 500 status code? – chris85 Apr 15 '18 at 14:28
  • No errors at all. Quite frustrating i must say – Alex Maina Apr 15 '18 at 14:30
  • The status code is not a 500? – chris85 Apr 15 '18 at 14:33
  • Yes. It is not 500 – Alex Maina Apr 15 '18 at 14:57
  • And you don't even get a `
    ` in the source of the page?
    – chris85 Apr 15 '18 at 15:08
  • Yes. not even a `
    `. Has it got to do with the url e.g., when i conduct a search the resultant url looks like `http://localhost/searchmodel.php?choice=title&term=malaria&mysearch=Submit+Query` and when i click on a page number the url looks like this `http://localhost/searchmodel.php?page=2`
    – Alex Maina Apr 15 '18 at 15:20
  • Line 8 is `echo ($pagenumber = isset($_GET['page']) ? $_GET['page'] : 1) . '
    ';` though so you should always be getting at least a `
    ` unless there is a fatal error
    – chris85 Apr 15 '18 at 15:25
  • line 8 returns the correct page number. But below it is a blank page where i expect results from the search – Alex Maina Apr 15 '18 at 15:35
  • So you do get a `
    ` in the source code?? I don't think your `blank page` description is accurate.
    – chris85 Apr 15 '18 at 15:39
  • I don't get a '
    ' because it is concactenated as part of the echo. What do you suggest i do to solve my problem?
    – Alex Maina Apr 15 '18 at 15:47
  • I don't know what your problem is. I don't know how your code executes and your description is confusing. You say the page is blank but you also state `But below it is a`, how can something be below nothing?? – chris85 Apr 15 '18 at 15:53

1 Answers1

1

change this line

echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";

to

<a href = "?choice=<?php echo $_GET['choice'].'&term='.$_GET['term'].'&mysearch=Submit+Query&page=' . $x; ?>"><?php echo $x;?></a>

The problem lies with the query link.

tom sawyer
  • 47
  • 1
  • 2
  • 9