Having a problem with my script. What is happening is when you enter your search term and hit search page refreshes and only displays the search form NO results. I then added a line to check to see if search term ( $var = $_GET['SearchTerm'] ) was being passed from search form to processing script and it was. Also connection to database checked out. I have searched Google and this forum for days which got me as far as i have gotten, but am still not seeing what is wrong. Any help would be wonderful.
$query is suppose to select column named links and keywords and display data in link column as result. Based on my searching of the script i think the problem is somewhere in the $query any ideas?
Search Form
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results.Please try again. If you need help please <a href='../help/search-help.php'>click here.</a></p>";
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["link"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
All of the code which appears to be processing the results of the SQL query are contained within an if() statement that only evaluates to true if the query returned no rows. In other words, the logic that dictates whether all of your code gets executed doesn't make much sense.
Also: your code is vulnerable to SQL injection attacks and/or just plain SQL errors. Reference: security.database.sql-injection
All of the code which appears to be processing the results of the SQL query are contained within an if() statement that only evaluates to true if the query returned no rows. In other words, the logic that dictates whether all of your code gets executed doesn't make much sense.
Also: your code is vulnerable to SQL injection attacks and/or just plain SQL errors. Reference: security.database.sql-injection
Ok understand SQL injection, however this script is going on a internal server on a network with no web access so SQL injection is not an issue. Will read up on that tho for my other projects.
Based on your reply worked with script some and now will display results, however is not passing results pass page 1.
As well is displaying (code below) reguardless if there is a result or not..... ??
PHP Code:
if ($numrows == 0);
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a></p>";
Full Reworked Script
PHP Code:
// Get the search variable from URL
$var = $_GET['SearchTerm'] ;
echo "<p>You searched for: "" . $var . """; // display what the person searched for
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=8;
// check for an empty string and display a message.
if ($trimmed == "")
{
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search term!</p>";
exit;
}
// Connect to the database.
require_once ('includes/connections/mysql_nfacts.php');
// Build SQL Query
$query = "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"
order by keywords";
if ($numrows == 0);
{
echo "<h4>Results</h4>";
echo "Sorry, your search: "" . $trimmed . "" returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><p>";
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// begin to show results set
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["link"];
echo "$title <br />" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "<br /><a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 8</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
I have spent the whole day searching books i have, web and this forum with not much luck, maybe i am searching for the wrong thing. I have figured out that problem lies with search var not being passed to the other pages but can not see where exactly the problem is. Can anyone shine some light on this ?
Second issue is stumping me as well why would this echo even tho result results are being displayed. this one i don't know where to begin searching for a solution. Any help would be welcome.
PHP Code:
if ($numrows == 0);
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a></p>";
Do the other pages use the same code? Is the variable $_GET['q'], or $_GET['SearchTerm']?
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
$var = $_GET['SearchTerm'] ;
echo "<p>You searched for: "" . $var . """; // display what the person searched for
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=8;
// check for an empty string and display a message.
if ($trimmed == "")
{
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search term!</p>";
exit;
}
// Connect to the database.
require_once ('includes/connections/mysql_nfacts.php');
// Build SQL Query
$query = "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"
order by keywords";
if ($numrows == 0);
{
echo "<h4>Results</h4>";
echo "Sorry, your search: "" . $trimmed . "" returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><p>";
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// begin to show results set
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["link"];
echo "$title <br />" ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "<br /><a href=\"search.php?s=$prevs&q=$var\">;
Prev 8</a>;";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
Second, you still have the same problem I mentioned - all of your code is inside the if() statement that checks if there are ZERO results. So even if you get rid of the semicolon that doesn't belong, your code will execute none of your code after you execute the SQL query and store the number of rows inside $numrows.
Second, you still have the same problem I mentioned - all of your code is inside the if() statement that checks if there are ZERO results. So even if you get rid of the semicolon that doesn't belong, your code will execute none of your code after you execute the SQL query and store the number of rows inside $numrows.
ok found it and fixed i think is this right?
PHP Code:
// Connect to the database.
require_once ('includes/connections/mysql_nfacts.php');
// Build SQL Query
$query = "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"
order by keywords";
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "Sorry, your search: "" . $trimmed . "" returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
I get the first 8 results displayed correctly . This output shows me that there are more than 8 results
PHP Code:
echo "<p><br />Showing results $b to $a of $numrows</p>";
When i click link to see the next 8 results the page is empty. I have been searching last two whole days everywhere on how to add pagination, use pagination any thing on pagination and can't figure it out.
Problem is in the links or the whole pagination part of script, i think.. at this point i am just guessing. Gonna go work at it some more. Any help to get on the right track welcomed.
Last edited by darkangel2001lv; 11-20-2012 at 03:16 PM.
found error in links to old searchterm var. Changed that however now when I click to see the next 8 results next page displays the same results not the next 8. hmmmm...
Revised Script
PHP Code:
<?php
// Get the search variable from URL
$var = $_GET['SearchTerm'] ;
echo "<p>You searched for: "" . $var . "" <br />"; // display what the person searched for
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=8;
// check for an empty string and display a message.
if ($trimmed == "")
{
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search term!</p>";
exit;
}
// Connect to the database.
require_once ('includes/connections/mysql_nfacts.php');
// Build SQL Query
$query = "SELECT link, keywords FROM products WHERE keywords like \"%$trimmed%\"
order by keywords";
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "Sorry, your search: "" . $trimmed . "" returned zero results. Please try again. If you need help please <a href='../help/search-help.php'>click here.</a><p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// begin to show results set
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["link"];
echo "$title <br />" ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "<br /><a href=\"search.php?s=$prevs&SearchTerm=$var\">;
Prev 8</a>;";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
You don't set $s = $_GET['s'] anywhere, so it will always be empty in your if statement.
Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail I'd rather be a comma, then a full stop. User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning
Changed that however now when I click to see the next 8 results next page displays the same results not the next 8. hmmmm...
PHP Code:
if (empty($s)) {
So, ask yourself ... will $s BE empty? Where is it coming from? I'll bet you can $_GET a clue from that
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
Hee hee, only somewhat related, but it reminds me of a story from the board here, where someone explained how they got their username ...
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
So, ask yourself ... will $s BE empty? Where is it coming from? I'll bet you can $_GET a clue from that
Ok logic tells me that the $s should never be empty, due to it should be the string that came back from $query based on $_GET['SearchTerm'] am i correct? If this is the case found this while searching changed it to what my search var. something like this,
Bookmarks