-1

I have a search bar and a sql database with 3 movies in

Name:Star Wars: Description: Contains the word group
Name:301: Description: Contains the word group
Name:destroyer: Description: Contains the word group

BUT when ever I search for the word "group"

I only get one result usually:

here is my php code:

 if(!isset($_POST['search']))
{
    header("Location:index.php");

}

$search_sql="SELECT * FROM php_item WHERE Name LIKE '%".$_POST['search']."%' OR Description LIKE '%".$_POST['search']."%'";
$search_query = mysql_query($search_sql);
if(mysql_num_rows($search_query)!=0)
{

$search_rs= mysql_fetch_assoc($search_query);
 }


?>


          </p>
          <p>&nbsp; Search Results</p>
          <?php

          if(mysql_num_rows($search_query) != 0){
              do{ ?>

              <p>
              <?php echo $search_rs['Name']; ?>

             <?php     }while($searchr_rs=mysql_fetch_assoc($search_query));





          } else {
            echo "No Results Found";  
          }





          ?>
7
  • 4
    You are vulnerable to SQL injection attacks
    – Marc B
    May 1, 2014 at 18:41
  • remove the first $search_rs= mysql_fetch_assoc($search_query); May 1, 2014 at 18:41
  • @AbhikChakraborty: -1. op is using a do/while loop, so that first fetch is necessary
    – Marc B
    May 1, 2014 at 18:42
  • @MarcB yes i know, thats not an issue for me atm May 1, 2014 at 18:42
  • its because the the desciption of 301 contains the word group? May 1, 2014 at 18:44

1 Answer 1

0

EDIT changed $search_rs to $searchr_rs in my old post & now it will work

<?php
if(!isset($_POST['search']))
{
    header("Location:index.php");

}

$search_sql="SELECT * FROM php_item WHERE Name LIKE '%".$_POST["search"]."%'  OR Description LIKE '%".$_POST["search"]."%' ";
$search_query = mysql_query($search_sql);
//print_r($search_query);
if(!$search_query){
die(mysql_error());
}
if(mysql_num_rows($search_query)>0)
{
 echo '<p>&nbsp; Search Results</p>';

              while($searchr_rs=mysql_fetch_array($search_query))
              {

             echo ' <p>';
              echo $searchr_rs['Name'];
    echo '</p>';
                 }





          } else {
            echo "No Results Found";  
          }




?>
5
  • it just made it worse haha May 1, 2014 at 18:52
  • @user3586915 HOw ? have u tried this code? what error you get? May 1, 2014 at 18:53
  • undefined variable: $search_rs.. BUT i added isset to the front.. im getting no errors but nothing is displaying but im not getting the no results found so it is search just not displaying May 1, 2014 at 18:54
  • @user3586915 Use my edited answer no need to use isset at $search_rs because you have typed the name as $searchr_rs which I have updated May 1, 2014 at 19:18
  • when i didnt add isset i was getting errors.. saying undefined variable.. therefore i added isset to get rid of them errors then i started to get results that werent showung May 1, 2014 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.