Jump to content

Search box help needed


Snatch

Recommended Posts

Hi, i've created a search box on my web site using the code bellow to allow users to search for a product. At the moment it works like a peach however, I would like it to query both the name field and the type field of the products table.

 

It's bugging me because i'm sure this is very simple but so far no luck with anything i've tried. Thanks in advance for your help!

 

<?php

 

include "connection.php";

 

//Get the word submitted by the form

$searchTitle = $_GET["search"];

if (!empty($searchTitle))

{

print "Looking for products containing $searchTitle <br><br/>";

// create query 

$query = "SELECT * FROM products WHERE name like '%$searchTitle%'";

//print $query;

// execute query

$result = mysql_query($query) or die ("Error in query");

 

// see if any rows were returned

if (mysql_num_rows($result)>0) {

echo "<div id=sortactions>".

            "Order results by: ".

"<a href='search.php?order=name'>Name / </a>".

            "<a href='search.php?order=price'>Price</a></div>";

 

 

    while ($row = @ mysql_fetch_array($result)) {

    //while($row = mysql_fetch_row($result)) {

        echo "<div id=browsestyle><table width=80% border=0>" .

  "<tr>" .

  "<td width=10% valign=top rowspan=9><span id=imgpad><img src=".$row["image"]." height=50 width=50 /></span></td></tr>" .

  "<tr><td width=25% valign=top><strong>Type: </strong></td><td width=75% valign=top>". $row["type"] ."</td></tr>" .

"<tr><td width=25% valign=top><strong>Name: </strong></td><td width=75% valign=top><a href = 'getprod.php?prodid=" . $row["id"] ."'>". $row["name"] ."</a></td></tr>" .

"<tr><td width=25% valign=top><strong>Price: </strong></td><td width=65% valign=top>" . $row["price"] . "</td></tr>" .

"</table></div>" ;

    }

}

 

else {

      // print status message

    echo "No Results Found!";

}

 

// free result set memory

mysql_free_result($result);

 

// close connection

mysql_close($conn);

}

?>

Link to comment
Share on other sites

Well, if you simply wanted to check both fields for the search term, then it would just be a case of modifying your query:

 

$query = "SELECT * FROM products WHERE name like '%$searchTitle%' or type like '%$searchTitle%'";

 

However, you may want an option for the user to select which fields to search in. In which case, you will need to pass an additional value through the url, which is the field to search.

 

Also, dont forget that you'll be needing to protect yourself from any malicious users - you'll need to validate the user input.

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.