PDA

View Full Version : Msql Search form loses values on next page


sonny
08-07-2009, 07:19 PM
Hi I set up a MsQL search form that has a field choice and text box, the search result part
works fine on the first page.

The problem: is when I click a next page link I receive the following error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource LINE 14

My Question: is how do you carry over the results from page to page,
I thought php self target just does that

any Idea what I did below wrong? also did I make it safe enough to use publicly?

Thanks
Sonny


<?

require "./connect.php";

//-------------------------------------
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Edit $data to be your query (I Don't understand this part I have two values Field and search string)
$results = mysql_query("SELECT * FROM visits WHERE upper($field) LIKE'%$find%'");
$rows = mysql_num_rows($results);


//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;


// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>

<html>
<head>
<title>Search Results - <?=$find?></title>
<style>
A { font-family: Arial; color: #008080; font-size: 10pt;}
A:hover { color: #C0C0C0; text-decoration: none;}
td { font-family: Arial; color: #000080; font-size: 10pt;}
p { font-family: Arial; color: #000080; font-size: 10pt;}
</style>

</head>
<body>
<div><center><table width="600" border="0"><tr><td colspan=5>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seached for:
<input type="text" name="find" value="" /> in field
<Select NAME="field">
<Option VALUE="city">City</option>
<Option VALUE="region">Region</option>
<Option VALUE="country">Country</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form></td></tr>
</table>
</center>
</div>
<div><center><table width="600" border="0"><tr>

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// We preform a bit of filtering
$find= strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$results = mysql_query("SELECT * FROM visits WHERE upper($field) LIKE'%$find%' $max");

//And we display the results
while($row = mysql_fetch_assoc($results))
{
$city=$row['city'];
$region=$row['region'];
$country=$row['country'];


echo "<tr><td>$city</td>
<td><b>$region</b></td>
<td><b>($country)</b></td></tr>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($results);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b><b><font color=ff0000> .$find";
echo "</font></b>";
}
?>
</td></tr>
</table></center></div>
</body>
</html>

Fumigator
08-07-2009, 07:40 PM
Without exception you need to be verifying the query execution to make sure there were no errors. Something like:


$query = "SELECT * FROM visits WHERE upper($field) LIKE'%$find%'";
$results = mysql_query($query);
if (!$results) {
die('Query error! Query: $query<br>Error:' . mysql_error());
}


Note I assign your query string to a variable so it can be displayed along with the query error.

sonny
08-07-2009, 08:19 PM
Without exception you need to be verifying the query execution to make sure there were no errors. Something like:


$query = "SELECT * FROM visits WHERE upper($field) LIKE'%$find%'";
$results = mysql_query($query);
if (!$results) {
die('Query error! Query: $query<br>Error:' . mysql_error());
}


Note I assign your query string to a variable so it can be displayed along with the query error.

This is the syntax error
LIKE'%%'' at line 1

This is what I do not understand, and what I think is also causing a problem


//Edit $data to be your query
$query = "SELECT * FROM visits WHERE upper($field) LIKE'%$find%'";
$results = mysql_query($query);
if (!$results) {
die('Query error! Query: $query<br>Error:' . mysql_error());
}


It says edit data with your query, but when I do that I get all sorts of errors
Do you know why results are not being carried after I click a next page link?

if so can you explain why?

Thanks
Sonny

Fumigator
08-07-2009, 09:28 PM
This is the syntax error
LIKE'%%'' at line 1


Do you know why results are not being carried after I click a next page link?

if so can you explain why?

The value of your variable $find is obviously blank. It's probably blank because whoever wrote this script assumed something about your PHP configuration that isn't true-- he/she assumed "register globals" would be turned on. PHP now defaults register globals to OFF, as it is a security risk. You need to replace $find with $_POST['find'] everywhere in your script. (and $field, and $searching, and $search.)

Your script has some rudimentary validation to catch when someone submits a blank form down below:


//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}


But not before the query up top. You need it both places.

sonny
08-07-2009, 10:19 PM
The value of your variable $find is obviously blank. It's probably blank because whoever wrote this script assumed something about your PHP configuration that isn't true-- he/she assumed "register globals" would be turned on. PHP now defaults register globals to OFF, as it is a security risk. You need to replace $find with $_POST['find'] everywhere in your script. (and $field, and $searching, and $search.)

Your script has some rudimentary validation to catch when someone submits a blank form down below:


//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}


But not before the query up top. You need it both places.

OK thank you very much, I will go over everything you mention, I am so confused about how to do this, at least now I have a somewhat idea.

Thanks for taking time to help!
Sonny

sonny
08-09-2009, 05:41 AM
Fumigator

Surprisingly I finally got this working with my most advanced pagination code
by adding a $field link into the page links along with the query, and calling it
from a outside page, it works perfect, but I know its not the best way to do
it.

I must have tried for hours and hours, to get that pagination code I posted
above to work from within the same page, I could not do it, I know I will have
to be able to do that if I want to use a page for multiplex things one day.

I tried replacing everything with $_POST['find'] and others you mentioned etc
but got so many errors doing it wrong I guess, I shut the computer off and
went to the bar.