0

I'm doing very simple pagination and even with isset in place, i'm getting

Notice: Undefined index: page on line 45

. I know what undefine index is, but I have no clue how to fix it in this code because the variable page is triggered in href.

table.php

<?php
$perpage=3;
if(isset($_GET['page'])){
    $page=$_GET['page'];
}
else{
    $page=1;
}
$offset=($_GET['page']-1)*$perpage;
$sql="SELECT* FROM `companystructures` LIMIT ".$offset.",".$perpage."";
$result=mysqli_query($db,$sql);
?>
    <table id="myTable" class="rwd-table table table-striped table-hover tablesorter" >

  <tr>
      <th>ID</th>
    <th>Name</th>
      <th>Description</th>
        <th>Type</th>
    <th>Address</th>
    <th>Country</th>
        <th>Time Zone</th>
  </tr>

  <?php while($company=mysqli_fetch_array($result)){ ?>
  <tr>
      <td data-th="ID"><?=$company['id'];?></a></td>
      <td data-th="Name"><?=$company['title'];?></td>
    <td data-th="Description"><?=$company['description'];?></td>
    <td data-th="Type"><?=$company['type'];?></td>
    <td data-th="Address"><?=$company['address'];?></td>
    <td data-th="Country"><?=$company['country'];?></td>
    <td data-th="Time Zone"><?=$company['timezone'];?></td>
  </tr>

    <?php };?>

</table>


      <div class='col-md-6 pull-right'>
<!-- Pagination -->
            <?php
$sql="SELECT * FROM `companystructures`";
$result=mysqli_query($db,$sql);
$total_rows=mysqli_num_rows($result);
$total_pages=ceil($total_rows/$perpage);
echo "<a href='home.php?page=1'>First</a>";
echo "<span style='margin:5px;'>";
for($i=1;$i<=$total_pages;$i++){
echo "<a href='home.php?page=$i'>$i</a>";
echo "<span style='margin:5px;'>";
}
echo "<a href='index.php?page=$total_pages'>last</a>";
?>

        </div>
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST` or `$_GET` data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Feb 10 '17 at 00:36
  • This is not a duplicate! I know what an undefined index is! How can a variable be known when it's actually triggered by onClick action? We get the data by clicking on a link. So of course it triggers an undefined index. I've set isset in place to avoid that. But still getting the error – D. Fisher Feb 10 '17 at 00:37
  • 1
    Yeah, it is a dupe. In `$offset=($_GET['page']-1)*$perpage;` you use your get variable, when you probably mean to use `$page`. – Qirel Feb 10 '17 at 00:49
  • @Qirel: Well would you look at that. – D. Fisher Feb 10 '17 at 01:10

0 Answers0