?
Solved

Database search results gives undefined index

Posted on 2006-03-30
8
Medium Priority
?
232 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:charlieb01
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16337555
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
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 16337559
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
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 2000 total points
ID: 16337587
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 16340091
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 16340120
That will not solve his problem as he would then also need to make his page navigation out of forms.

-r-
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 16340135
A hidden input won't work, since he's not submitting a form when clicking the page links...
0
 

Author Comment

by:charlieb01
ID: 16342997
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
 

Author Comment

by:charlieb01
ID: 16345408
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question