Jump to content

[SOLVED] Check Input From Search Query


limitphp

Recommended Posts

I'm building a simple search feature on my website.

if I use the LIKE '%$query%' to do the search.....would I need to use anything more to check the input besides:

 

<?php
function check_input($value)
{
	// Stripslashes
	if (get_magic_quotes_gpc())
	  {
	  $value = stripslashes($value);
	  }
	  $value = mysql_real_escape_string($value);		  
	return $value;
}
?>

Link to comment
Share on other sites

For searching, it is usually a good idea to split the query into multiple parts and exclude common words like "the" "and" "or" etc.

 

Your check input function is fine. But for the actual searching part I would do something like this:

 

<?php
    function parse_query($query, $col) {
        if (strlen($query) < 3) 
            return false; 

       if (strstr($strng, " ") === false)
            return " $col LIKE '%" . check_input($string) . "%' ";

       $words = explode(" ", strtolower($string));
       $statement = array();
       $notAllowed = array("and", "the", "or"); // add more if you like
       foreach ($words as $word) {
             if (strlen($word) > 2 && !in_array($word, $notAllowed)) {
                 $statement[] =  " $col LIKE '%" . check_input($word) . "%' ";
             }
       }

       return implode(" OR ", $statement);
    }
?>

 

Should put you on the right track. That way they are more like keywords and are more for searching. Questions let me know. The above is not tested.

Link to comment
Share on other sites

For searching, it is usually a good idea to split the query into multiple parts and exclude common words like "the" "and" "or" etc.

 

Your check input function is fine. But for the actual searching part I would do something like this:

 

<?php
    function parse_query($query, $col) {
        if (strlen($query) < 3) 
            return false; 

       if (strstr($strng, " ") === false)
            return " $col LIKE '%" . check_input($string) . "%' ";

       $words = explode(" ", strtolower($string));
       $statement = array();
       $notAllowed = array("and", "the", "or"); // add more if you like
       foreach ($words as $word) {
             if (strlen($word) > 2 && !in_array($word, $notAllowed)) {
                 $statement[] =  " $col LIKE '%" . check_input($word) . "%' ";
             }
       }

       return implode(" OR ", $statement);
    }
?>

 

Should put you on the right track. That way they are more like keywords and are more for searching. Questions let me know. The above is not tested.

 

Is $col what column they are searching in?  Like band, song, and then any would equal band,song?

 

Where do you define $strng?

 

 

Link to comment
Share on other sites

Sorry, I should have looked better, and yes that is what $col should be. Here is updated code:

 

<?php
    function parse_query($string, $col) {
        if (strlen($string) < 3) 
            return false; 

       if (strstr($strng, " ") === false)
            return " $col LIKE '%" . check_input($string) . "%' ";

       $words = explode(" ", strtolower($string));
       $statement = array();
       $notAllowed = array("and", "the", "or"); // add more if you like
       foreach ($words as $word) {
             if (strlen($word) > 2 && !in_array($word, $notAllowed)) {
                 $statement[] =  " $col LIKE '%" . check_input($word) . "%' ";
             }
       }

       return implode(" OR ", $statement);
    }

    $query = mysql_query("SELECT xx FROM tablexx WHERE " . parse_query($_POST['query'], "band") . " ORDER BY band");
?>

 

Would also be the usage.

Link to comment
Share on other sites

I could assume that some songs might be called "Ax Grinder" or some other short 2 letter word.....

in that case, maybe I should let 2 letter words through....

 

if so, would I exclude:

if (strlen($string) < 3)

            return false;

 

and strlen($word) > 2

?

 

Also, I see that you change the query to lowercase, but will it match the mysql entry if its not in lowercase?

 

like zeppelin matching Zeppelin?

Link to comment
Share on other sites

I could assume that some songs might be called "Ax Grinder" or some other short 2 letter word.....

in that case, maybe I should let 2 letter words through....

 

if so, would I exclude:

if (strlen($string) < 3)

            return false;

 

and strlen($word) > 2

?

 

Also, I see that you change the query to lowercase, but will it match the mysql entry if its not in lowercase?

 

like zeppelin matching Zeppelin?

 

Yes they will match, mysql is case insensitive. If you want to allow any size letters but disallow certain words, remove the strlen checking portions. I set it all to lowercase for the in_array portion.

Link to comment
Share on other sites

So, if all they type in is "or the", it will never get:

$statement[] =  " $col LIKE '%" . check_input($word) . "%' ";

 

if statement doesn't get set, will that mess up the query?

 

 

 

It would return all results.

 

<?php
    function parse_query($string, $col) {
        if (strlen($string) < 3) 
            return false; 

       if (strstr($strng, " ") === false)
            return " $col LIKE '%" . check_input($string) . "%' ";

       $words = explode(" ", strtolower($string));
       $statement = array();
       $notAllowed = array("and", "the", "or"); // add more if you like
       foreach ($words as $word) {
             if (strlen($word) > 2 && !in_array($word, $notAllowed)) {
                 $statement[] =  " $col LIKE '%" . check_input($word) . "%' ";
             }
       }

       if (count($statement) > 0)
            return implode(" OR ", $statement);
       else
            return false;  
    }

    $where = parse_query($_POST['query'], "band");
    if (!$where) 
          die("Invalid search parameters.");

    $query = mysql_query("SELECT xx FROM tablexx WHERE " . $where . " ORDER BY band");
?>

 

Should do the error checking properly.

 

To answer the red river, yes. If you want to do exact matches you will need to modify the OR to AND so it will only pull up songs with both keywords in it. You could even add a checkbox option to use AND or OR on that part if you wanted to.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.