2016-05-27 7 views
1

でPHPとSQLを使用したページを増やす現在、私はブログで忙しいですが、ページが増え、オフセットと制限があります。ボタンをクリックすると、ページをカウントアップして各ページに10行を表示することになっています。私はこれがあります。

<?php 

$rowsPerPage = 10; //number of results you want to display 
$num = $_GET["page"]; //set the offset to start w/the num. of results (good for paging) 
$offset = ($num - 1) * $rowsPerPage; // to offset the limit count 
$sql = "SELECT * FROM `posts` ORDER BY `id` DESC LIMIT ".$rowsPerPage." OFFSET ".$offset.""; 
$result = $conn->query($sql); 
while($row = $result->fetch(PDO::FETCH_ASSOC)) { 
    echo '<div class="post-preview"> 
      <a href="posts.php?id='.$row["id"].'"> 
       <h2 class="post-title"> 
        '.$row["title"].' 
       </h2> 
       <h3 class="post-subtitle"> 
        '.$row["content"].' 
       </h3> 
      </a> 
      <p class="post-meta"><a href="#">'.$row["creator"].'</a> | '.$row["date"].'</p> 
     </div> 
     <hr>'; 
    }  
    ?> 

をしかし、唯一のdomain.com/index.php?page=1で動作しているようですが、それは私が「/index.php?page=1」を削除し、罰金などをロードします私は、誰かが私を助けることができると思います

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-10' at line 1' in /home/u9778802/public_html/blog/index.php:49 Stack trace: #0 /home/u9778802/public_html/blog/index.php(49): PDO->query('SELECT * FROM `...') #1 {main} thrown in /home/u9778802/public_html/blog/index.php on line 49 

:私は次のエラーを取得する$ _GETセットせずにインデックスに進みます。

+2

をので、 '$ sql'をエコーし​​、クエリはあなたのようなルックスを建てか見てください。 '$ _GET ['page']' ** is NOT **が定義されている場合、 '$ offset'は' -10'になり、負のオフセットを持つことはできません。これは、 'undefined index'警告を生成していることを意味しています。これはおそらく抑止されている/隠されています。 –

答えて

1

あなたはSQLインジェクションに開いている間、論理的な問題は、使用して固定することができます。

$num = (isset($_GET["page"]) and is_int($_GET["page"])) ? $_GET["page"] : 1;