I have one html page with a form for searching. The form action posts to a new php page.
I get results but when i click next(next page) i get the undefined variable error.
i know undefined variable error means i'm using a variable that has not be defined but i believe i have defined the variables i'm using. Below is my code
<?php
if (isset($_POST['q'])) {
$var = $_POST['q'];
}
if(isset($_POST['txttablename'])){
$tbl_name= $_POST['txttablename'];
}
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="cms2"; // Database name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//This code will obtain the required page number from the $_GET array. Note that if it is not present it will default to 1.
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if
//This code will count how many rows will satisfy the current query.
$query = ("select * from $tbl_name where business_title like '%" .$var. "%' ");
$result = mysql_query($query) or die("cannot select ");
//$query_data = mysql_fetch_row($result);
$numrows = mysql_num_rows($result);
//This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.
$rows_per_page = 4;
$lastpage = ceil($numrows/$rows_per_page);
//This code checks that the value of $pageno is an integer between 1 and $lastpage.
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if
//This code will construct the LIMIT clause for the sql SELECT statement.
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "select * from $tbl_name where business_title like '%" .$var. "%' $limit " ;
$result = mysql_query($query) or die("cannot select DB");
//$query_data = mysql_fetch_row($result);
$numrows = mysql_num_rows($result);
while ($info=mysql_fetch_array($result)) {
echo "<div class='standard'>";
echo "<b>";
echo $info['business_title'];
echo "</b>";
echo "<br />";
}
//Finally we must construct the hyperlinks which will allow the user to select other pages. We will start with the links for any previous pages.
if ($pageno == 1) {
echo "";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>first</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>prev</a> ";
}
// if
//Next we inform the user of his current position in the sequence of available pages.
echo " ( Page $pageno of $lastpage ) ";
//This code will provide the links for any following pages.
if ($pageno == $lastpage) {
echo "";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>next</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>last</a> ";
} /
?>
This is the error message i get
Notice: Undefined variable: tbl_name in C:\wamp on line 29
Notice: Undefined variable: var in C:\wamp on line 29