Jump to content

Simple search feature - Displays all items


laflair13

Recommended Posts

This has me pulling my hair out! I have tried everything I could find and it is still displaying all the items in the database. 

 

I have a search area on my site and when I put in a search keyword, the results.php page has every item on it. I have searched and searched and I cannot figure out why it is doing it.
 
Any help would be greatly appreciated.
 
My search form
<form id="search-form123" action="results.php" method="POST">
     <div class="offlajn-ajax-search-inner">
          <input type="text" name="keyword" id="search-area123" value="" autocomplete="off" placeholder="Search Here...">
          <input type="submit" name="Submit" value="Search" id="search-area123">
          <input type="hidden" name="Submit" value="com_search">
 </form>

results.php ( on the top of page )

<?php 
include_once('mysql_connect.php');


if (!isset($_POST['search'])) 
$keyword = $_POST['search'];
$search_sql="SELECT * FROM new_equip WHERE itemname LIKE  '%" .$keyword. "%'";
$search_query=mysql_query($search_sql);
if(mysql_num_rows($search_query)!=0)  {
$search_rs=mysql_fetch_assoc($search_query);
}

$eid = $row['id'];
$itemname = $row['itemname'];

?>

results.php ( where the items are displayed )

<?php if (mysql_num_rows($search_query)!=0){ do {?>
     <p><a href="new-product.php?Item=<?php echo $search_rs['id']; ?>"><?php echo $search_rs ['itemname']?></a></p>
<?php } while ($search_rs=mysql_fetch_assoc($search_query)) ;
         } else { echo "No Results Found";
   } ?>

And I will say that I am learning mysqli to convert my site over. I am just wanting to do it all at once so there are no issues. 

 

If anyone can help with that, I would greatly appreciate it because again I have search and tried everything and I cant get it working.

Thank you for your response but wouldnt this get passed as the input search?

 

on form                               

<input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/>
Then on results.php
if (!isset($_POST['search'])) 
$keyword = $_POST['search'];

I tried changing it and it is still displaying all the items.

 

form

<input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/>
<input type="submit" name='Submit' value="Search" id="search-area123" />
<input type="hidden" name='Submit' value="com_search" />

results.php

if (!isset($_POST['keyword']))

$keyword = $_POST['keyword'];
$search_sql="SELECT * FROM new_equip WHERE itemname LIKE  '%" .$keyword. "%'";
$search_query=mysql_query($search_sql);
if(mysql_num_rows($search_query)!=0)  {
$search_rs=mysql_fetch_assoc($search_query);
}

just a minor point, but your search form is specifying what will be displayed/gotten on the page and should use the get method. this will make it easier to propagate and combine the search/keyword term in url's since it will already be a $_GET parameter.

I think it was fine using POST. I think the problem was here in your results.php:

if (!isset($_POST['keyword']))
  $keyword = $_POST['keyword'];
  //...

You're saying if the $_POST['keyword'] IS NOT SET, to USE $_POST['keyword'] which obviously you can't since it doesn't exist

 

Try removing the ! from the isset (which makes it NOT isset())

Still getting all the items showing up.

 

<?php 
include_once('mysql_connect.php');

if (isset($_POST['keyword']))

$keyword = $_POST['keyword'];
$search_sql="SELECT * FROM new_equip WHERE itemname LIKE  '%" .$keyword. "%'";
$search_query=mysql_query($search_sql);
if(mysql_num_rows($search_query)!=0)  {
$search_rs=mysql_fetch_assoc($search_query);
}

$eid = $row['id'];
$itemname = $row['itemname'];
?>

Try wrapping the whole thing in braces so it will only execute if $_POST['keyword'] exists.

if (isset($_POST['keyword'])) { //add brace

  $keyword = $_POST['keyword'];
  $search_sql="SELECT * FROM new_equip WHERE itemname LIKE  '%" .$keyword. "%'";
  $search_query=mysql_query($search_sql);
  if(mysql_num_rows($search_query)!=0)  {
    $search_rs=mysql_fetch_assoc($search_query);
  }

  $eid = $row['id'];
  $itemname = $row['itemname'];
} //add brace

That means your form is incorrect somehow. Show us your entire form, including the form open/close tags. Did you change the form back to using POST?

 

also, if you implemented the braces I showed you you shouldn't get that error you mentioned, unless you didn't show us all of the code.

Form code

<form id="search-form123" action='results.php' method="POST">
      <div class="offlajn-ajax-search-inner">
        <input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/>
        <input type="submit" name='Submit' value="Search" id="search-area123" />
        <input type="hidden" name='Submit' value="com_search" />
    <div id="search-area-close123"></div>
   <div id="ajax-search-button123"><div class="magnifier"></div></div>
   <div class="ajax-clear"></div>
 </div>
</form>

Top of results.php

<?php 


include_once('mysql_connect.php');


if (isset($_POST['keyword'])) { //add brace


  $keyword = $_POST['keyword'];
  $search_sql="SELECT * FROM new_equip WHERE itemname LIKE  '%" .$keyword. "%'";
  $search_query=mysql_query($search_sql);
  if(mysql_num_rows($search_query)!=0)  {
    $search_rs=mysql_fetch_assoc($search_query);
  }


  $eid = $row['id'];
  $itemname = $row['itemname'];
} //add brace


?>

Where results are displayed

<?php
if (mysql_num_rows($search_query)!=0){
  do {?>
       
  <p><a href="new-product.php?Item=<?php echo $search_rs['id']; ?>"><?php echo $search_rs ['itemname']?></a></p>
    
<?php   } while ($search_rs=mysql_fetch_assoc($search_query)) ;
  
  } else {
    echo "No Results Found";
    } 
?>

Well after A LOT of trial and error I figured out how to make it work in mysqli. 

 

If you could, please double check my work to make sure it is a good way to do it?

 

form

<form id="search-form123" action='results.php' method="GET"><input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/>
<input type="submit" name='Submit' value="Search" id="search-area123" />
<input type="hidden" name='Submit' value="com_search" />
</form>

Top of results.php

<?php  


$db = new mysqli("localhost","admin","pass","database"); 


if(!$db) {
    die('sorry we are having some problbems');
}


// SET GETTER AS A VARIABLE
$searchTerm = mysqli_real_escape_string($db,$_GET['keyword']);


if ( empty($searchTerm))
{
echo("no key words searched please try again");
}
else
{
$sql = mysqli_query(
    $db,
    sprintf(
        "SELECT * FROM new_equip WHERE itemname LIKE '%s'",
        '%'. $searchTerm .'%'
    )
);
}
?>

where results are displayed

<?php 
 while($ser = mysqli_fetch_array($sql)) {
  echo "<p><a href='new-product.php?Item=$ser[id]'>$ser[itemname]</a></p>";
    }
?>

I want to say thank you to everyone who took the time with the help on this. I as lost. 

Ok, I just changed the method="POST" to method="$_GET" and I got this in the url

 

results.php?keyword=capper&Submit=Search&Submit=com_search

The form method should be "GET" and not "$_GET" (although you get away with probably as GET is the default).

 

Having changed the form, $_GET is what you should now be using in the php code instead of $_POST

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.