Help in pagination problem in searching

This is my code:

<?php $db_host = "localhost"; $db_user = "root"; $db_pwd = "111"; $database = "data"; //$term=$_POST["term"]; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can’t connect to database"); if (!mysql_select_db($database)) die("Can’t select database"); if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * 25; // sending query $term = $_POST['term']; ///line19 $result = mysql_query("SELECT Assets_No,Asset_Description,Ownership FROM asset WHERE Asset_Class LIKE '%".$term."%' OR Location LIKE '%".$term."%' OR Serial_number LIKE '%".$term."%' OR PIC LIKE '%".$term."%'LIMIT $start_from ,25"); //select * from the_table where 1st_field like \"%$trimmed%\" //echo $term; if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); // $No = mysql_num_fields($result); echo "

Table: Asset Maintenance_RBMP

"; echo ""; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo ""; //echo ""; } echo "\n"; // printing table rows //$results = mysql_query("SELECT COUNT(*) FROM asset"); while($row = mysql_fetch_row($result)) { echo ""; // $row is array… foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo ""; echo "\n"; } mysql_free_result($result); echo"
{$field->name}{$field->No}
$cell
"; ?> <?php $result = mysql_query("SELECT COUNT($term) FROM asset WHERE Asset_Class LIKE '%".$term."%' OR Location LIKE '%".$term."%' OR Serial_number LIKE '%".$term."%' OR PIC LIKE '%".$term."%'LIMIT $start_from ,25"); //echo $term; $row = mysql_fetch_row($result); $total_records = $row[0]; $total_pages = ceil($total_records / 25); for ($i=1; $i<=$total_pages; $i++) { echo "".$i." "; }; ?>

///////////////////////////
I got a problem after searching the data in the first page the data come out is correct but after next page the result is incorrect data.
And shown an error is Notice: Undefined index: term in C:\xampp\htdocs\xampp\new2\search.php on line 19
Please help!!!

Well, it is saying that you have no field called “term” on your HTML page that calls this script.
OR, data inside of the TERM field is bad. First, is that field in your calling form? And, is it straight
text?

Lastly, I just noticed that you are using $GET’s and $POST’s on the same page. Is your form set up for POSTING or GETTING. You can only use one method.

GETTING is used by passing the data in the URL of the post. Seldom used as the users see your data structure and hackers love using that info. POSTING hides it and the PHP code pulls it from the posted page.

Let’s start with your tag… method=“POST” or method=“GET” … ???

I changed the GET to POST. However still get the same problem.
That is mine searchform is working very well, but after searching, my pagination doesn’t work anymore. I shows the first pag of results, but when I want to see page 2, it just shows all the records.

How should i solve this problem.
Please help!

Well, first, you used POST, so that should be the METHOD used in your form.

Next, you showed us code that pulls data and displays it. You did NOT display any code for HTML. These both work together. So, you need to understand what you are working with. (If you already know this, please excuse me.)

A FORM in HTML is POSTED to a PHP page. This PHP page uses the posted data to display the needed data from the database. (You code does this!)

Usually, the PHP would be INSIDE of more HTML. This html would alter the displayed PREVIOUS/NEXT button’s code to select the next needed segments. (Your code does not do this.)

So, to fix it, you must alter your current FORM that calls this code and place this code INSIDE of your HTML page. OR, you can use the PHP to OUTPUT a copy of your HTML code.

I am sure you are confused at this point. Basically, once PHP output is sent back to the browser ALL PHP is gone. So, pressing a button just executes the HTML code, all PHP does not exist at that point.

So, how to fix it? Well, either use SESSION variables and use them inside of your HTML displayed to the browser, or reprogram your PHP to output the needed buttons to point at the next group of data.

Think about this and then ask your next questions.

I solved this.
Thank you!! :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service