【问题标题】:PHP pagination SQL Syntax error when page= -1page= -1时PHP分页SQL语法错误
【发布时间】:2014-04-10 20:55:27
【问题描述】:

当我检查此 url 以进行页面分页时,分页工作并在页面中显示页面结果和 page=+1:

mydomain/search.php?page=1

mydomain/search.php?page=2

但是当我检查这个网址时:

mydomain/search.php?page=-1

mydomain/search.php?page=-2

我看到这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20, 10' at line 1

我使用这个分页代码打印结果:

// If number of results is bigger than the maximum number
  // of search results set in config we start the pagination
  if ( $results > $conf['search_results'] )

   {

    // Calculate the first number of page to show
    // This makes the list of pages numbers smaller
    if ((($page*$conf['search_results'])-($conf['search_results']*5)) >= 0) 
     $first=($page*$conf['search_results'])-($conf['search_results']*5);
    else 
     $first=0;

    // Calculate the last element of the pagination list
    if ((($page*$conf['search_results'])+($conf['search_results']*6)) <= $results) 
     $last =($page*$conf['search_results'])+($conf['search_results']*6);
    else 
     $last = $results;

    @    $i=$first/$conf['search_results'];

    // Previous link
    if ($page > 0)
     {
      $pagenum = $page - 1;
      echo ' <a style="float:left;" href="' . URL . '/search.php?page=' . $pagenum . '&amp;' . $session->fetch('listingsearchvariablespage') . '">PRE</a> | ';
     }

    // Middle pagination
    for ( $step = $first; $step < $last; $step=$step+$conf['search_results'] )

     {

      if ( $i == $page )

       {

    $pagenum = $i+1;
    echo ' <span class="warning">' . $pagenum . '</span> | ';
    $i++;

       }

      else

       {

    $pagenum = $i+1;
    echo ' <a href="' . URL . '/search.php?page=' . $i . '&amp;' . $session->fetch('listingsearchvariablespage') . '">' . $pagenum . '</a> | ';
    $i++;

       }

     }

    // Next link
    if ($page - (($results / $conf['search_results']) - 1) < 0)
     {
      $pagenum = $page+1;
      echo ' <a style="float:right;" href="' . URL . '/search.php?page=' . $pagenum . '&amp;' . $session->fetch('listingsearchvariablespage') . '">NEXT</a>';      
     }

   }

现在,我该如何解决这个负数错误并防止任何攻击?

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    我会推荐一些简单的东西,例如:

    if($page < 1)
        $page = 1;
    

    【讨论】:

    • 同意。我还要添加 $page = (int)$_GET['page'];防止在 url 中传递除整数以外的任何内容
    • @andrew,是的 - 我有点假设他已经这样做了,但我猜你永远不知道。
    【解决方案2】:

    LIMIT -20,10 是无效的语法 - 你不能有一个负偏移量。您应该将该值限制在有效的页面范围内。

    【讨论】:

    • 我知道!我需要防止这种情况发生任何攻击。我认为阻止负数。
    猜你喜欢
    • 2014-08-22
    • 2019-12-27
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多