1

I am trying to create a pagination page that will display only one result per page from a search query, but after displaying the first result, the next result or page will give me an error that no valid input was inputtedFirst Result Shows right stuffOther result(2) showing this error

 <?php
    define("ROW_PER_PAGE",1);
    $search = '';
      if (!empty($_POST['search'])) {
         $search = $_POST['search'];

                  /* Search Entry */

                $keywords =  explode(' ', $search);
                $arr_length = count($keywords);
                $sub_query = '';

                    for ($i = 0; $i < $arr_length; $i++) {
                      if ($i == 0) {
                        $sub_query = $sub_query . 'sym_body LIKE "%' . $keywords[$i] . '%" OR sym_tags LIKE "%' . $keywords[$i] . '%"';
                        }else {
                        $sub_query = $sub_query . ' OR sym_body LIKE "%' . $keywords[$i] . '%" OR sym_tags LIKE "%' . $keywords[$i] . '%"';
                      }
                    }

                  /* Query */
                $query = 'SELECT * FROM symptoms WHERE ' . $sub_query; 


                  /*Pagination Code Starts */
                        $per_page_htm = '';
                        $page = 1;
                        $start = 0;
                          if(!empty($_GET['r'])) {
                             $page = $_GET['r'];
                             $start = ($page - 1) * ROW_PER_PAGE;
                           }     
                  $limit = "LIMIT " . $start . "," . ROW_PER_PAGE;

                /* Getting Row Count Tested Working */
              $p_query = 'SELECT COUNT(*) FROM symptoms WHERE ' . $sub_query;
              $pag_stnt = $con->query($p_query);
              $row_count = $pag_stnt->fetchColumn();

                echo "<h4>You have $row_count result(s)</h4>";

                    if (!empty($row_count)) {
                      $per_page_htm .= "<div style='text-align:center;margin:20px 0px;'>";
                      $page_count = ceil($row_count / ROW_PER_PAGE);
                        if ($page_count > 1) {
                          $self = $_SERVER['PHP_SELF'];
                            for($i=1; $i<=$page_count; $i++){
                                if($i==$page){
                                    $per_page_htm .= "<a href='".$self."?r=".$i."' class='btn-page current'>".$i."</a>";
                                  }else{
                                    $per_page_htm .= "<a href='".$self."?r=".$i."' class='btn-page'>".$i."</a>"; 
                                }
                            }
                        }
                        $per_page_htm .= "</div>";




                          $n_query = 'SELECT * FROM symptoms WHERE ' . $sub_query . ' '.$limit ;

                            foreach ($con->query($n_query) as $row) {

                              $sym_cat_id = $row['cat_id'];
                              $sym_pro = $row['sym_pro'];

                              $sym_body = $row['sym_body'];
                              $sym_ans = $row['sym_answer'];



                      ?>
                    <!-- Symptom Area-->

                      <div class="panel-group results">
                          <div class="panel panel-success">
                            <div class="panel-heading">
                             <p><?php //echo $cat_title;?></p> 
                             <?php echo $search; ?>
                            </div>
                            <div class="panel-body">
                              <?php echo $sym_body; ?>
                              <p><h4>Answer</h4>
                              <?php echo $sym_ans;?></p>
                              <p><h4>Was the answer helpful</h4></p>

                                  <p>
                                    <a href="index.php" class="btn btn-info pull-left"> Yes</a>
                                    <button type="button" class="btn btn-danger pull-right">No</button>
                                  </p>
                            </div>
                          </div>
                      </div>
                          <?php
                            }
                         }else{  
                        echo '<script>
                            setTimeout(function(){
                              swal({
                                title: "Sorry!",
                                text: "Your Query is not in our database",
                                type: "info"

                              }, function(){
                                window.location = "index.php";
                              });
                            }, 1000);
                            </script>';}
              echo $per_page_htm;
         }else{
            echo '<script>
              setTimeout(function(){
                swal({
                  title: "Oops!",
                  text: "Please enter a valid value!",
                  type: "error"

                }, function(){
                  window.location = "index.php";
                });
              }, 1000);
          </script>';} ?>
Victor Alagwu
  • 87
  • 2
  • 12

1 Answers1

1

... after displaying the first result, the next result or page will give me an error that no valid input was inputted

That's because when you hit the 2nd, 3rd, ... page(after navigating from the 1st page), the $_POST array would be empty i.e. $_POST['search'] won't be set. Since you're not sending any sensitive data with the form, use GET instead of POST in the method attribute of the form, like this:

<form action="..." method="get">

and get the user inputted data like this:

if (!empty($_GET['search'])) {
    $search = $_GET['search'];
    ...

Subsequently, you need to attach that search query in each of your pagination links, so that the search query would be available when you hop from page to page.

// your code
for($i=1; $i<=$page_count; $i++){
    if($i==$page){
        $per_page_htm .= "<a href='".$self."?r=".$i."&search=".urlencode($search)."' class='btn-page current'>".$i."</a>";
    }else{
        $per_page_htm .= "<a href='".$self."?r=".$i."&search=".urlencode($search)."' class='btn-page'>".$i."</a>"; 
    }
}
// your code

Sidenote: Learn about prepared statement because right now your queries are susceptible to SQL injection attack. Also see how you can prevent SQL injection in PHP.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37