0

Following is my search.php code:

<?php
if(isset($_POST['action']) && $_POST['action'] == "Search Question")
{
$search = mysql_real_escape_string(trim(htmlspecialchars($_POST['search'])));   
$err = array();

if(isset($search))
    {   
            if(empty($search))
                $err[] = "Please write your question name";
            elseif(is_numeric($search))
                $err[] = "Wrong values entered";
        }   
        if(!empty($err))
        {
            foreach($err as $er)
            {
                echo "<font color=red>$er<br/></font>"; 
            }
        }
        else
        {

            $search_sql = mysql_query("SELECT * FROM questions WHERE q_name LIKE '%$search%'");


            //$search_sql = mysql_query("SELECT * FROM questions WHERE MATCH(q_name, answer) AGAINST  '$search' ");             
            $numserach = mysql_num_rows($search_sql);

            if($numserach == 1)
            {
                echo "<font color='green'><strong>$numserach Question found</strong></font><br/>";
                while($result = mysql_fetch_array($search_sql))
                {
                    $subid = $result['sub_id'];
                    $id = $result['id'];
                    $qquestionsname = $result['q_name'];

                    ////////////////////////////////////////////////// SEARCH SHOW START HERE ///////////////////////
                    //include("toppagi.php");
                    $questions = $result['q_name']; 
                    echo "<a href='answerdetails.php?id=$id&subid=$subid&questionname=$qquestionsname'>$questions</a><br/>";
                    //include("bottompagi.php");
                    ////////////////////////////////////////////////// SEARCH SHOW END HERE ///////////////////////
                }
            }
            else
            {
                echo "<font color=red>No Result Found</font>";
            }

        }   
    }
    ?>

In my database I'm trying to searching "q_name" filed where all my questions name is stored. Like.. Accounting, book, O level, test o level etc.

Well, But If i search anything like ac OR te then it doesn't show any result. I need if i search ac or te or something else then it's show some result..

Thanks in Advance:)

4 Answers 4

1

use this line to

  if($numserach > 0)
  {.....

instead of

  if($numserach == 1)
  {.....
Sign up to request clarification or add additional context in comments.

3 Comments

Nice @Rab Nawaz It's Working now! But I've another issue... After found my search item then i see every details BUT when i go back then Firefox show "DOCUMENT Expired. " WhY?
@Shibbir, this is how POST method work. You can offer the search result in another page so when the user go back they won't get this Document Expired.
@Shibbir, Well, you can express your gratitude to this guy by upvoting + accepting his answer
1

As others have already mentioned, your problem by replacing

if($numserach == 1)

with

if($numserach > 0)

Very IMPORTANT :

I do this on every question where the OP's Database handling is severely bad as a way of advocating a better practice.

Please take some time and read this fine tutorial on how to use PDO. You 'll save yourself future embarrassment when your project gets hacked (if this was professional work) and you'll learn a new thing to protect yourself and give yourself a better chance of improving the performance of your application.

2 Comments

Random excerpt from the tutorial: The Oracle database is used by many 'enterprise' companies but these days there are sleeker options. Funny xD
@ÁlvaroG.Vicario, :D | this is one of the best easy-to-understand PDO tutorials I've ever seen.
1

Replace

if($numserach == 1)

with

if($numserach > 0)

because at the moment you are only showing the results if there is exactly one term found. If it finds more than one, no results will be shown.

Comments

0

Change the instruction below to the newest:

if($numserach == 1) --> if($numserach >0) 

and re-execute the script.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.