Link to home
Start Free TrialLog in
Avatar of charlieb01
charlieb01

asked on

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
Avatar of Roonaan
Roonaan
Flag of Netherlands image

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-
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.
ASKER CERTIFIED SOLUTION
Avatar of Tomeeboy
Tomeeboy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hiteshgupta1
hiteshgupta1

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)
That will not solve his problem as he would then also need to make his page navigation out of forms.

-r-
A hidden input won't work, since he's not submitting a form when clicking the page links...
Avatar of charlieb01

ASKER

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
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