分页链接无法正常工作


Pagination links not working correctly

嗨!我想对 HTML 表单中的结果进行分页。一切正常,直到我实际单击应该将我带到下一组结果的链接。这是我表单的 PHP 方面:

<?php
/*########################## D.B Connection ##########################*/
include("db_conex.php");    
/*########################## Error reporting ##########################*/   
ini_set('display_errors', '1'); 
/*########################## Set output to default at zero ##########################*/
$search_output = "";
/*########################## Pagination Script ##########################*/
/*########################## Input  Clean ##########################*/
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
// Query one get total no of rows
if($_POST['filter1'] == "nsw"){
    $sqlCommand = " SELECT COUNT(id) FROM nsw WHERE type = '$searchquery' ";
}
else if($_POST['filter1'] == "nt"){
    $sqlCommand = " SELECT COUNT(id) FROM nt WHERE type = '$searchquery' ";
}   
else if($_POST['filter1'] == "qld"){
    $sqlCommand = " SELECT COUNT(id) FROM qld WHERE type = '$searchquery' ";
}
else if($_POST['filter1'] == "sa"){
    $sqlCommand = " SELECT COUNT(id) FROM sa WHERE type = '$searchquery' ";
}   
else if($_POST['filter1'] == "vic"){
    $sqlCommand = " SELECT COUNT(id) FROM vic WHERE type = '$searchquery' ";
}   
else if($_POST['filter1'] == "tas"){
    $sqlCommand = " SELECT COUNT(id) FROM tas WHERE type = '$searchquery' ";
}   
else if($_POST['filter1'] == "wa"){
    $sqlCommand = " SELECT COUNT(id) FROM wa WHERE type = '$searchquery' ";
}   
else if($_POST['filter1'] == "act"){
    $sqlCommand = " SELECT COUNT(id) FROM act WHERE type = '$searchquery' ";
}   
$query = mysql_query($sqlCommand , $mysqlConnection);
$row = mysql_fetch_row($query);
//total row count
$rows = $row[0];
//number of results displayed on each page
$page_rows =1;
//this tells us the page number of Our last page
$last =ceil($rows/$page_rows);
//ensures $last can not be less than 1
if($last < 1)
    $last=1;
//determine page number
$pagenum = 1;
//Get pagenum from URL variable if present, else it is = 1
if(isset($_GET['pn']))
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);       
//makes sure pagenum is not below 1 our more than Our $last page
if ($pagenum < 1){
    $pagenum = 1;
}
else if ($pagenum > $last){
    $pagenum = $last;
}   
//set range of rows to query for the chosen $pagenum    
$limit = 'LIMIT '.($pagenum - 1)* $page_rows.','.$page_rows;
//sql query again
/*########################## Input Check & Clean ##########################*/
if(isset($_POST['searchquery']) && $_POST['searchquery'] != "")
    {
//$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
    }
//$sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM nt WHERE type = 'diesel mechanic' ORDER BY date_added DESC  $limit";
/*########################## sql query  ##########################*/
if($_POST['filter1'] == "nsw"){
    $sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM nsw WHERE type = '$searchquery' ORDER BY date_added DESC  $limit";
}
else if($_POST['filter1'] == "nt"){
    $sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM nt WHERE type = '$searchquery' ORDER BY date_added DESC  $limit";
}
else if($_POST['filter1'] == "qld"){
    $sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM qld WHERE type = '$searchquery' ORDER BY date_added DESC  $limit";
}
else if($_POST['filter1'] == "sa"){
$sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM sa WHERE type = '$searchquery' ORDER BY date_added DESC  $limit";
}
else if($_POST['filter1'] == "vic")
        {
$sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM vic WHERE type = '$searchquery' ORDER BY date_added DESC  $limit"; 
         }
else if($_POST['filter1'] == "tas")
         {
$sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM tas WHERE type = '$searchquery' ORDER BY date_added DESC  $limit";
         }
else if($_POST['filter1'] == "wa")
          {
$sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM wa WHERE type = '$searchquery' ORDER BY date_added DESC  $limit"; 
          }
else if($_POST['filter1'] == "act")
          {
$sqlCommand = " SELECT company , ref , title , description ,address , date_added FROM act WHERE type = '$searchquery' ORDER BY date_added DESC  $limit";
         }
/*########################## End Sql Query ##########################*/
$query = mysql_query($sqlCommand , $mysqlConnection);
// displays current page number and total number of pages
$tline1 = "Results: (<b>$rows</b>)";
$tline2 = "Job <b>$pagenum</b> of <b>$last</b>";    
//Establish $pagination variables
$paginationCtrls = '';
//if there is more than 1 page of results
if($last !=1)
        {
            //check which page we are on and displaying link or not.
            if($pagenum>1)
            {
                $previous = $pagenum -1;
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
                //make clickable number links: left side
                for($i = $pagenum-1; $i < $pagenum; $i++)
                    {
                        if($i > 0)
                        {
                            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                        }
                    }
            }
            //show current page number without link function
            $paginationCtrls .= ''.$pagenum.' &nbsp; ';
            //make clickable number links: right side
            for($i = $pagenum+1; $i <=$last; $i++)
                    {
                        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                        if($i >=$pagenum=4)
                        {
                            break;
                        }
                    }
//check to see if we are on last page
if ($pagenum != $last)
            {
                $next = $pagenum + 1;
                $paginationCtrls .= '&nbsp; &nbsp; &nbsp; <a href ="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
            }                   
        }
        //how many records in DB        
    $count = mysql_num_rows($query);
if($count > 0){
$search_output .= "<hr >results for <strong>'$searchquery'</strong><hr >";
$list = '';
while($row = mysql_fetch_array($query))
            {
            $id = $row["ref"];
             $id1 = $row["company"];
             $id2 = $row["description"];
             $id3 =$row["date_added"];
             $id4 =$row["title"];
             $id5 =$row["address"];
/*############# End Execute Command #############*/     
   $search_output .= "<div class ='result'><span class ='ref'>Job ref no: $id </span><br><br><b>Company: </b>$id1<br><br><b>Location: </b>$id5<br><br><b>Date posted: </b>$id3<br><br><b>Job Title: </b>$id4<br><br><br><b>Job Description</b><br><br> $id2<br></div><br><br>";
                } // close while
            }
            else 
        {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
        }
/*############# End OutPut #############*/  
// Close your database connection 
//mysqli_close($mysqlConnection);
?>

任何帮助将不胜感激。

这一行:

    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);

您使用 $_POST 获取搜索查询,当您导航到另一个页面(即单击其中一个分页链接)时,POST 数据会消失。因此,您需要将查询作为 GET 参数包含在分页 URL 中:

    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].
            '?pn='.$i.'&amp;searchquery='.$searchquery.'">'.$i.'</a> &nbsp; ';

您需要从 $_POST 和 $_GET 中获取查询:

    if (isset($_POST['searchquery'])) {
            $searchquery = $_POST['searchquery'];
    }
    elseif (isset($_GET['searchquery'])) {
            $searchquery = $_GET['searchquery'];
    } else {
            // Invalid request, error handling code here
    }
    // Safety precautions
    $searchquery = strip_tags($searchquery);

注意:当您在MySQL查询中使用它时,您也应该转义查询,否则注入将是可能的!阅读更多: http://php.net/mysqli_real_escape_string .或者更好的是,使用预准备语句:http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

编辑09.09.13:您需要对$ _POST['过滤器']做同样的事情