I have developed a searchable database.
Since I have added paging code, the searchability no longer works properly.
If I enter no search restrictions the first 10 results are shown then when I click the next button the following error comes up:
Notice: Undefined index: cat in c:\inetpub\wwwroot\practice_bite\SearchProj.php on line 30
Notice: Undefined index: region in c:\inetpub\wwwroot\practice_bite\SearchProj.php on line 31
Notice: Undefined index: search in c:\inetpub\wwwroot\practice_bite\SearchProj.php on line 32
Error, query failed
cat, region, and search are my search criteria and worked perfectly before I added the paging.
If I enter search restrictions, the search works, but, the paging tells me there are 2 pages when there should only be one, and when I click page 2 or the next button, the same error comes up as before.
If I remove the searchability of the database and just return all results the paging works fine.
Any ideas what is going wrong?
<?php
mysql_connect ("localhost", "root", "******");
mysql_select_db ('good_practice');
$cat=$_POST["cat"];
$region=$_POST["region"];
$search=$_POST["search"];
// how many rows to show per page
$rowsPerPage = 10;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = "SELECT * FROM project AS p, regions AS r, cats as c
WHERE p.region = r.region AND c.id = p.id AND org LIKE '%$search%' AND p.region LIKE '$region' AND '$cat'='0'
OR p.region = r.region AND c.id = p.id AND org LIKE '%$search%' AND $region=0 AND c.catid LIKE '$cat'
OR p.region = r.region AND c.id = p.id AND org LIKE '%$search%' AND p.region LIKE '$region' AND c.catid LIKE '$cat'
OR p.region = r.region AND c.id = p.id AND org LIKE '%$search%' AND $region=0 AND '$cat'='0'
GROUP BY p.id
ORDER BY org
"
.
" LIMIT $offset, $rowsPerPage";
$result1 = mysql_query($query) or die('Error, query failed');
// how many rows we have in database
$query = "SELECT COUNT(id) AS numrows FROM project";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
if(mysql_num_rows($result1)>0)
{
while($r = mysql_fetch_array($result1))
{
$id=$r["id"];
$org=$r["org"];
$address1=$r["address1"];
$address2=$r["address2"];
$address3=$r["address3"];
$city=$r["city"];
$pcode=$r["pcode"];
$tel=$r["tel"];
$fax=$r["fax"];
$email=$r["email"];
$web=$r["web"];
$name=$r["name"];
$surname=$r["surname"];
$image=$r["image"];
$updated=$r["updated"];
$regdesc=$r["regdesc"];
echo "<table width=800><tr><td><hr></table><table><tr bgcolor=#ffffff><td width=300 valign=top><font face=Arial, Helvetica, sans-serif size=3><b>$org</b><font face=Arial, Helvetica, sans-serif size=2>
";
if($address1>0)print "<br>$address1";
if($address2>0)print "<br>$address2";
if($address3>0)print "<br>$address3";
echo "<br> <font face=Arial, Helvetica, sans-serif size=2>
$city<br>$pcode<br><i>Region:</i> $regdesc";
if($tel>0)print "<br><i>Tel:</i> $tel<br>";
if($fax>0)print "<i>Fax:</i> $fax<br>";
print "<i>Email: </i><a href=mailto:$email>$email</a><br>";
print "<i>Contact Name: </i>$name $surname<br>";
print "<i>Web: </i><a href=http://$web target=_blank>$web</a><br>";
if($updated>0)print "<i>Updated:</i> $updated<br>";
echo"<font face=Arial, Helvetica, sans-serif size=2><b>Categories:</b><br>";
$result2 = mysql_query("SELECT catdesc, g.catid FROM def_cats AS d, cats AS g
WHERE g.catid = d.catid AND g.id = ".$r["id"]."
ORDER BY catdesc
")
or die ("Error - ".mysql_error()."");
while($r2=mysql_fetch_array($result2))
{ $catdesc=$r2["catdesc"];
echo "<font face=Arial, Helvetica, sans-serif size=2>$catdesc <br>";
}
echo"<td valign=top align=left width=115><font face=Arial, Helvetica, sans-serif size=2><img src='$image' width='100'><br>";
echo "<a href='searchprojMI.php?id=$id'>More project info</a></td>";
echo " <td width=15><td valign=top width=375>";
$result3 = mysql_query("SELECT * FROM practice AS p, project AS t, regions AS r
WHERE p.id = t.id AND t.region = r.region AND p.id = ".$r["id"]."
ORDER BY verified
")
or die ("Error - ".mysql_error()."");
while($r3=mysql_fetch_array($result3))
{
$gpid=$r3["gpid"];
$gptext=$r3["gptext"];
$gptitle=$r3["gptitle"];
$min_age=$r3["min_age"];
$max_age=$r3["max_age"];
$verified=$r3["verified"];
echo "<font face=Arial, Helvetica, sans-serif size=2><i>Practice Example Title: </i><b>$gptitle</b><br>
<i>Age range:</i> $min_age to $max_age <br><i>Updated:</i> $verified<br>";
echo "<a href='searchpracMI.php?gpid=$gpid'>More detail</a><br><hr>";
}
echo " </table>";
}
}
else
print "<br><br>Search terms too specific - please try again.";
?>