2017-07-01 78 views
1

我想创建一个分页页面,每个页面只显示一个搜索查询结果,但显示第一个结果后,下一个结果或页面会给我一个错误没有有效的输入输入被First Result Shows right stuffOther result(2) showing this errorPDO分页显示第一个结果后没有显示数据

<?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>';} ?> 
+0

当你点击第二,第三,...页时,你会得到什么错误? –

+0

我被要求输入一个有效的值..上面附加的图像 –

+0

这些页面上的查询出现了什么内容?您应该使用参数化查询。 – chris85

回答

1

...显示的第一个结果后,下一个结果或页面会给我没有有效的输入输入被

这是因为一个错误当您点击第2页,第3页...(从第1页导航之后)时,$_POST阵列将为空,即不会设置$_POST['search']。既然你不与形式发送任何敏感数据,使用GET代替POST形式的method属性,像这样:

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

,并获得用户输入这样的数据:

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

随后,您需要在每个分页链接中附加该搜索查询,以便在每个页面跳转时都可以使用搜索查询。

// 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 

旁注:了解prepared statement因为现在你的查询很容易受到SQL注入攻击。另见how you can prevent SQL injection in PHP

+0

谢谢,它工作 –