开发者

Pagination Error

Im trying to do pages for my search result.My Search function is working fine. However, when I click on the page number. This error appears (below) :

Notice: Undefined index: search in C:\wamp\www\I-Document\new.php on line 8 ERROR: Select from dropdown

This message only should appear when there is no input in dropdown and no search input. Im not sure how to correct this. Please help! Thank u

<?php

//connecting to the database
include 'config.php';


$search = mysql_escape_string($_POST['search']);

$dropdown = mysql_escape_string($_POST['dropdown']); 

if (empty($search) && empty($dropdown)) {
die("Please choose your Search Criteria");
  } 

  //max displayed per page
  $per_page = 10;

  //get start variable  
  $start = $_GET['start'];

  //count records
  $record_count = mysql_query("SELECT COUNT(*) FROM document WHERE $dropdown LIKE     '%$search%'");


  //count max pages
  $max_pages = $record_count / $per_page; 

  if (!$start)
    $start = 0;

    //display data
    $query = mysql_query("SELECT * 
                            FROM document 
                           WHERE $dropdown LIKE '%$search%' 
                           LIMIT $start, $per_page");

    echo "<b><center>Search Result</center></b><br>";

    $num=mysql_num_rows($query);

    if ($num==0)
      echo "No results found";
    else {
      echo "$num results found!<p>"; 
    }

  echo "You searched for <b>$search</b><br /><br /><hr size='1'>";
  echo "<table border='1' width='600'>
          <th>File Reference No.</th>
          <th>File Name</th>
          <th>Owner</th>
          <th>Borrow</th>
       </tr>";

  while ($rows = mysql_fetch_assoc($query)) {
    echo "<tr>";
    echo "<td>". $rows['file_ref']  ."</td>";
  开发者_开发知识库  echo "<td>". $rows['file_name'] ."</td>";
    echo "<td>". $rows['owner'] ."</td>";
    echo "<td><a href=add_borrower.php?id=" . $rows['id'] . ">Borrow</a></td>";
    echo "</tr>";
  }

  echo "</table>"; 

 //setup prev and next variables
 $prev = $start - $per_page;
 $next = $start + $per_page;

 //show prev button
 if (!($start<=0)) 
   echo "<a href='new.php?start=$prev'>Prev</a> ";

 //show page numbers
 //set variable for first page
 $i=1;

 for ($x=0;$x<$record_count;$x=$x+$per_page) {
   if ($start!=$x)
     echo " <a href='new.php?start=$x'>$i</a> ";
   else
     echo " <a href='new.php?start=$x'><b>$i</b></a> ";

   $i++;
}
}

//show next button
if (!($start>=$record_count-$per_page))
  echo " <a href='new.php?start=$next'>Next</a>";

?>


Looks to me like when you click on a link, you no longer have the POST data being sent (it becomes a simple GET request). You'll need to create a form, then have a submit button for each of the pages. Or have the search term in the Query String, like Google (google.com/search?q=your+search+term+here)


This is the offending line

$search = mysql_escape_string($_POST['search']);

Your logic goes

IF $_POST['submit'] IS NOT SET
THEN
    $search = mysql_escape_string($_POST['search']);

It's a pretty safe bet to say that if $_POST['submit'] is not set, then neither is $_POST['search']

Also, consider when you click one of your "page" links, that will issue a GET request and $_POST will be empty. You can either pass your POST parameters via GET along with the pagination data and look for them in either $_GET or $_POST (or $_REQUEST if you're feeling saucy), or create your pagination control as a "postable" form.


You already have all the code. You just need to rewrite your error condition.

  1. Get the input variables
  2. Encode them
  3. Test for presence of both or die() with error message

So from your description:

$search = mysql_escape_string($_POST['search']);

$dropdown = mysql_escape_string($_POST['dropdown']); 

if (empty($search) && empty($dropdown)) {
    die("ERROR: whatever");
}

In this case you can test with empty() even after the escaping, because it would be an empty string in any case.


if (isset($_POST["search"])){

$search = mysql_escape_string($_POST['search']);

}else{

$search = mysql_escape_string($_GET['search']);

}

do the same for $_POST['dropdown'] but when printing out each page link , you should add the &search= and &dropdown= in the href of the page number .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜