Search code examples
phpmysqlpaginationsearch-engine

Undefined index after clicking next in pagination. Search engine with fields and result's on a different page


Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I did a search engine with fields and the result is displayed on a different page. On the result page there is pagination, the problem is when i click next my query and field is undefined. I'm a newbie in php and i really need some help. oh, please please please

i don't know if i had to type this because you're experts out here but here it is anyway: the $query is the users input and the $field is the selected field

<?php
session_start();
require("config.php");
require("clean.php");

print "<body><h1>Jobs</h1>";

$query = $_GET['query'];
$field = $_GET['field'];

$rowsPerPage = 1;
$pageNum = 1;

if(isset($_GET['page'])) {
    $pageNum = clean($_GET['page']);
}

$offset = ($pageNum - 1) * $rowsPerPage;

$result = mysql_query("SELECT * FROM post WHERE upper($field) LIKE'%$query%'"."LIMIT $offset, $rowsPerPage") or die('Error, query failed'); 
while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
        print "<li>";
        print "<b>{$row['CNAME']}</b><br />";
        print "{$row['CITY']}, ";
        print "{$row['PROV']} (";
        print "{$row['CNTRY']}) <br /><br />";
        print "<u>{$row['POS']}</u><br />";
        print "{$row['REQ']}<br /><br />";
        print "<i>{$row['CNO']}</i>";
        print "</li>";
}
print "<br />";

$query = "SELECT COUNT(*) AS numrows FROM POST WHERE upper($field) LIKE'%$query%'";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
print "<HR><BR><CENTER>($numrows results found!)";
print "<BR><br>You are now in page $pageNum</CENTER>";

$maxPage = ceil($numrows/$rowsPerPage);

echo "<CENTER>Go to page : ";
for($page = 1; $page <= $maxPage; $page++) {
    echo "<a href='result7.php?page=$page'>$page</a> ";
}
$page=$page-1;

print "<br><br>Click <a href=\"home.php\">here </a> for new search";?>

Solution

  • Remember, your page doesn't know anything except what gets passed in the URL or POST. Your links to subsequent pages do not pass the $query or $field fields. You need to add those to the URL's you are creating for each link:

    for($page = 1; $page <= $maxPage; $page++) {
        echo "<a href='result7.php?page=$page&field=$field_escaped&query=$something_escaped'>$page</a> ";
    }
    

    where $query_escaped and $field_escaped are URL encoded versions of the field and query values you want to pass to subsequent pages.

    Note that there are all sorts of security problems with this code; you are pretty much asking for an SQL exploit if this were to appear in public.