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

if (isset($_POST['q'])) {
  $var = $_POST['q'];
}
 
if(isset($_POST['txttablename'])){
  $tbl_name = $_POST['txttablename'];
}

$var and $tbl_name are only given a value, if their POST counterpart is set. You should give them a default (empty) value. I usually do this:

$var = isset($_POST['q']) ? $_POST['q'] : '';
$tbl_name = isset($_POST['txttablename']) ? $_POST['txttablename'] : '';

Thanks pritaeas
The undefined error doesn't show again when i click on next, i get a cannot select db.

it fails at this point

$query = ("select * from $tbl_name where business_title like '%" .$var. "%' ");
$result = mysql_query($query) or die("cannot select ");

Since your default value is now an empty string, if you do not post a tbl_name, the select_db and query will fail.

Since your default value is now an empty string, if you do not post a tbl_name, the select_db and query will fail.

how do i retain the value?

($_POST['q']

comes from a different form and the value has to be retained till the form is closed.

Put it in $_SESSION.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.