knitex 0 Newbie Poster

Hey guys, I'm kind of new to php. So my problem is that my code just doesn't work and I can't figure out why. I had it working when had the original code when all it did was search and display the results but now I'm trying to add page numbers and limit the results per page and link the plot number to display on another page with a separate query. Right now the code doesn't even display the table so I was trying to figure what was wrong but I'm stumped.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Family Search</title>
<style type="text/css">
th {
	color: #900;
	height: 100%;
	border-right-width: 1px;
	border-right-style: solid;
	border-right-color: #666;
	border-left-color: #666;
}
tr {
	height: 100%;
	width: 100%;
	border-top-width: 1px;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-top-style: solid;
	border-right-style: solid;
	border-bottom-style: solid;
	border-top-color: #666;
	border-right-color: #666;
	border-bottom-color: #666;
}
</style>
</head>

<body>
<?php
    mysql_connect ("localhost", "root","root") or die (mysql_error());
    mysql_select_db ("FHSNL");
	//sets all values in form to php variables
    $surname = $_POST['surname'];
    $given = $_POST['given'];
	$maiden = $_POST['maiden'];
	$deathdate = $_POST['death'];
	$deathdaterange = $_POST['deathyearrange'];
	$deathbefore = $_POST['death'] - $deathyearrange;
	$deathafter = $_POST['death'] + $deathyearrange;
	$age = $_POST['age'];
	$birth = $_POST['birth'];
	$birthplace = $_POST['birthplace'];
	$deathplace = $_POST['deathplace'];
	$erected = $_POST['erected'];
	$region = $_POST['region'];
	$religion = $_POST['religion'];
	//divides into smaller sql pieces for filled in form input.
	if($surname)$piece[] .= "`SURNAME` LIKE '%$surname%'";
	if($given)$piece[] .= "`GIVEN` LIKE '%$given%'";
	if($maiden)$piece[] .= "`MAIDEN` LIKE '%$maiden%'";
	if($age)$piece[] .= "`AGE` LIKE '%$age%'";
	if($deathplace)$piece[] .= "`PLACE_OF_DEATH` LIKE '%$deathplace%'";
	if($region)$piece[] .= "`TOWN` LIKE '%$region%'";
	if($religion)$piece[] .= "`RELIGION` LIKE '%$religion%'";
	if($deathdate)$piece[] .= "`YEAR_OF_DEATH` BETWEEN '$deathbefore' AND '$deathafter'";
	//if submit button is pressed it runs the search query.
    if(isset($_POST['submit'])){
    
		if(isset($piece)){
		if (!(isset($pagenum))) 

 { 

 $pagenum = 1; 

 } 
  $sqlend = implode(" AND ",$piece);
  $sql = mysql_query("SELECT * FROM `HEADSTONESA` WHERE $sqlend");
  $rows = mysql_num_rows($sql);
  $page_rows = 25;
  $last = ceil($rows / $page_rows);
  if($pagenum < 1)
  {
  $pagenum = 1;
  }
  elseif($pagenum > $last)
  {
  $pagenum = $last;
  }
  
  $max = " LIMIT " .($pagenum - 1) * $page_rows .",". $page_rows;
  $sql_p = mysql_query("SELECT * FROM `HEADSTONESA` WHERE $sqlend $max") or die(mysql_error());
  //constructs the table.
	echo "<table><tr><th>ID</th><th>Plot Number</th><th>Given</th><th>Surname</th><th>Cemetery</th><th>Region<'/th><th>Town</th><th>Date of Death</th><th>Age</th><th>Date of Birth</th><th>Place of Birth</th><th>Place of Death</th><th>Erected By</th><th>Additional Info</th>";
	while($row = mysql_fetch_array($sql_p))
	{
		echo "<tr>";
    echo "<td>" .$row['ID'] . "</td>";
    echo '<td> <a href = "profile.php?pnum='.$row['ID'].'">' . $row['PLOT_NO'] . '</a></td>';
    echo "<td>".$row['GIVEN']."</td>";
    echo "<td>".$row['SURNAME']."</td>";
	$requested_id = $row['KEYS'];
	$town_id = $row['TOWN'];
	$region_id = $row['REGION'];
	$sql2 = mysql_query("SELECT * FROM `CEMETERIES` WHERE `KEYS` LIKE '$requested_id'");
	$row2 = mysql_fetch_array($sql2);
	echo "<td>".$row2['NAME']."</td>";
	echo "<td>".$row2['REGION']."</>";
	echo "<td>".$row2['TOWN']."</td>";
    echo "<td>".$row['D_OF_DEATH']."</td>";
    echo "<td>" .$row['AGE']. "</td>";
	echo "<td>" .$row['D_OF_BIRTH']."</td>";
	echo "<td>" .$row['PLACE_OF_BIRTH']."</td>";
	echo "<td>" .$row['PLACE_OF_DEATH']."</td>";
	echo "<td>" .$row['ERECTED_BY']."</td>";
	echo "<td>" .$row['ADD_INFO']."</td>";
	echo "</tr>";
		
	}
	echo "</table>";
	//displays page number of total pages
	echo "---Page $pagenum of $last--- <p>"
	if($pagenum == 1)
	{}
	else{
	echo "<a href = '{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a>";
	echo "";
	$previous = $pagenum - 1;
	echo "<a href = '{$_SERVER['PHP_SELF']}?pagenum=$previous'>". $previous. "</a>"
	;
	echo "";
	$previous2 = $pagenum - 2;
	echo "<a href = '{$_SERVER['PHP_SELF']}?pagenum=$previous2'>". $previous2."</a>";
	}
echo "..".$pagenum."..";
if($pagenum == $last)
{
}
else{
$nextpage = $pagenum + 1;
echo "<a href = '{$_SERVER['PHP_SELF']}?pagenum=$nextpage'>".$nextpage."</a>";
echo "";
$nextpage2 = $pagenum + 2;
echo "<a href = '{$_SERVER['PHP_SELF']}?pagenum=$nextpage2'>".$nextpage2."</a>";
echo "";
echo "<a href = '{$_SERVER['PHP_SELF']}?pagenum=$last'> Last--></a>":
}
	//if nothing found prints out the following.
    if(!$rows)
    echo "No results where found";
	
    }
}
//if no search terms entered prints the following.
else{
  echo "You must enter valid search terms";
}
    
    
    ?>
    
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Family Search</title>
<style type="text/css">
th {
	color: #900;
	height: 100%;
	border-right-width: 1px;
	border-right-style: solid;
	border-right-color: #666;
	border-left-color: #666;
}
tr {
	height: 100%;
	width: 100%;
	border-top-width: 1px;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-top-style: solid;
	border-right-style: solid;
	border-bottom-style: solid;
	border-top-color: #666;
	border-right-color: #666;
	border-bottom-color: #666;
}
</style>
</head>

<body>
<?php
 	mysql_connect ("localhost", "root","root") or die (mysql_error());
    mysql_select_db ("FHSNL");
    $id = $_GET['pnum'];
    $sql = mysql_query("SELECT * FROM `HEADSTONESA` WHERE `ID` = '$id'") or die(mysql_error());
    $results = mysql_fetch_array($sql);
    $requested_cem = $results['KEYS'];
    $requested_plot = $results['PLOT_NO'];
    $sql2 = mysql_query("SELECT * FROM `HEADSTONESA` WHERE `KEYS` = '$requested_cem' AND `PLOT_NO` = '$requested_plot'");
echo "<table><tr><th>ID</th><th>Plot Number</th><th>Given</th><th>Surname</th><th>Cemetery</th><th>Region</th><th>Town</th><th>Date of Death</th><th>Age</th><th>Date of Birth</th><th>Place of Birth</th><th>Place of Death</th><th>Erected By</th><th>Additional Info</th>";
$rows = mysql_num_row($sql2);
while($results2 = mysql_fetch_array($sql2));
{
echo "<tr>";
    echo "<td>" .$row['ID'] . "</td>";
    echo "<td>" . $row['PLOT_NO'] . "</td>";
    echo "<td>".$row['GIVEN']."</td>";
    echo "<td>".$row['SURNAME']."</td>";
	$requested_id = $row['KEYS'];
	$town_id = $row['TOWN'];
	$region_id = $row['REGION'];
	$sql2 = mysql_query("SELECT * FROM `CEMETERIES` WHERE `KEYS` LIKE '$requested_id'");
	$row2 = mysql_fetch_array($sql2);
	echo "<td>".$row2['NAME']."</td>";
	echo "<td>".$row2['REGION']."</>";
	echo "<td>".$row2['TOWN']."</td>";
    echo "<td>".$row['D_OF_DEATH']."</td>";
    echo "<td>" .$row['AGE']. "</td>";
	echo "<td>" .$row['D_OF_BIRTH']."</td>";
	echo "<td>" .$row['PLACE_OF_BIRTH']."</td>";
	echo "<td>" .$row['PLACE_OF_DEATH']."</td>";
	echo "<td>" .$row['ERECTED_BY']."</td>";
	echo "<td>" .$row['ADD_INFO']."</td>";
	echo "</tr>";
		
	}
echo "</table>";
?>

</body>
</html>

It could be something really simple that I'm just missing.
Thanks for the help