Database search results gives undefined index

Hi Everyone,

I have built a database table with names, birthday, and telephone number. I have an HTML page that allows the user to search by any one field and give a search criteria. (for example: user selects Birthday from drop down list and types April in the text box and presses the GO button - this will return all records with birthdays in April)

The actual work is done by a PHP page. Here is the code from both files:

FILE # 1

<!      ***      SEARCH.HTM      3-21-06***
<html>
<head>
<title>EXAMPLE - SEARCH</title>
</head>
<body bgcolor="#EAEBE5">

<form method="post" action="results.php" target="_self">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td bordercolor="#000000">
<p align="center">
<select name="metode" size="1">
<option value="name">Name</option>
<option value="telephone">Telephone</option>
<option value="birthday">Birthday</option>
</select> <input type="text" name="search" size="25"> &nbsp;<br>
Search database: <input type="submit" value="Go!!" name="Go"></p>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>



FILE # 2


<!      ***      RESULTS.PHP      3-21-06***
<html>
<head>
<title>EXAMPLE - TODAYS RESULTS</title>
</head>
<body bgcolor="#EAEBE5">
<center>
<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td width="60"><b>id</b></td>
<td width="100"><b>Name</b></td>
<td width="70"><b>Telephone</b></td>
<td width="150"><b>Birthday</b></td>
</tr>
<tr>
<td>
<?php
$hostname = "localhost"; // The DB server.
$username = "myusername"; // The username for the database.
$password = "mypassword"; // The password for the username.
$usertable = "details"; // The name of the table.
$dbName = "mydatabase"; // This is the name of the database.

if (!isset($_GET['page'])) {
  $page = 1;
  } else {
    $page = $_GET['page'];
}

//Define the number of results per page
$max_results = 10;

//Figure out the limit for the query based on the current page number
$from = (($page * $max_results) - $max_results);


MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");
?>
<?php

$firstvar = $_POST['metode'];
$secondvar = trim($_POST['search']); //trim the whitespace from the stored variable

//Set up the Query
$query="SELECT * FROM details WHERE $firstvar LIKE '%$secondvar%' LIMIT $from, $max_results";

//Perform the query on only the current page number's results
$result = mysql_query($query) or die("Couldn't execute query");

while ($row = mysql_fetch_array($result))
{
$variable1=$row["id"];
$variable2=$row["name"];
$variable3=$row["telephone"];
$variable4=$row["birthday"];
//table layout for results

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td>$variable4</td>");
print ("</tr>");
}

//Figure out the total number of results in database search
$total_results = mysql_result(mysql_query("SELECT COUNT(*) FROM details WHERE $firstvar LIKE '%$secondvar%'"),0);

//Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

//Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

//Build PREVIOUS Link
if ($page >1) {
  $prev = ($page -1);
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a>";
}

for ($i = 1; $i <= $total_pages; $i++) {
  if (($page) ==$i) {
    echo "$i ";
} else {
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>";
  }
}

//Build NEXT Link
if ($page < $total_pages) {
  $next = ($page +1);
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}

echo "</center>";

?>
</table>

<?php
//below this is the function for no record!!
//here is the error message to be displayed
$XX = "Sorry, No Records Found, to search again please return to the search page";
//check to see if any records were found
if (mysql_num_rows($result)==0)      //IF NO ROWS ARE RETURNED THEN DISPLAY NO RECORDS MESSAGE
{
?> <br>
<?php
print ("$XX");
}
?>
</center>
</body>
</html>

The first 10 results come out fine.

The problem is, if I get more than 10 results and I click on the page 2 link or the Next link, I get an error:

Undefined index: metode on line 42
Undefined index: search on line 43

These are the variables for the values that the user entered on the Search page.

I feel like I am close to an answer but just cannot figure out this last part.

I would be happy to put both the search and results into one PHP file but I can't get that working either.

Any Help? Please!

Thanks,
Charlie
charlieb01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
First of all, you should have all the relevant search details in your links to next and previous pages:

  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";

Php is not magically going to remember what the parameters where for your search.

Secondly, hyperlinks are processed through GET requests, and therefor querystring variables are not going to be available through $_POST, but through $_GET only.

-r-
0
TomeeboyCommented:
Once they click your links to access the other pages, your variables are essentially gone, unless you pass them to the next page using the query string.
0
TomeeboyCommented:
An example of what to do with one of your links:

echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&metode=" . urlencode($firstvar) . "&search=" . urlencode($secondvar) . "\">$i</a>";

Then you want to change the top part of your script to:

$firstvar = urldecode($_REQUEST['metode']);
$secondvar = trim(urldecode($_REQUEST['search'])); //trim the whitespace from the stored variable

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

hiteshgupta1Commented:
u have problem with one value only
u can use a hidden text box to store the value of $firstvar
that will solve ur problm easily(though it's not the best solution to go with)
0
RoonaanCommented:
That will not solve his problem as he would then also need to make his page navigation out of forms.

-r-
0
TomeeboyCommented:
A hidden input won't work, since he's not submitting a form when clicking the page links...
0
charlieb01Author Commented:
OK, I used the suggestion fom Tomeeboy and I can now get the pages.

However, I am not getting the word Previous after I move to a set of results after the first page.

This is what I get after clicking on search:

1 [ 2 ] [3]>Next>>

When I click on either Next or [ 2 ] , I get the following:

<[ 1 ] 2 [ 3 ]>Next>>

I would expect (and would like) to get:

<<Previous<[ 1 ] 2 [ 3 ]>Next>>


 (the pertinete part of the code is shown below)

//Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

//Build PREVIOUS Link
if ($page >1) {
  $prev = ($page -1);
//  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a>";
      echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&metode=" . urlencode($firstvar) . "&search=" . urlencode($secondvar) . "\"><<Previous</a>";
}

for ($i = 1; $i <= $total_pages; $i++) {
  if (($page) ==$i) {
    echo "$i ";
} else {
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&metode=" . urlencode($firstvar) . "&search=" . urlencode($secondvar) . "\"> [ $i ] </a>";
  }
}

//Build NEXT Link
if ($page < $total_pages) {
  $next = ($page +1);
  //echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&metode=" . urlencode($firstvar) . "&search=" . urlencode($secondvar) . "\">>Next>></a>";
}

echo "</center>";


Any ideas?

Thanks,
Charlie
0
charlieb01Author Commented:
Ok, I did a little digging and discovered the answer was to use &lt as in the following line:

echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&metode=" . urlencode($firstvar) . "&search=" . urlencode($secondvar) . "\">&lt;&lt;Previous</a>";

Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.