Snatch Posted August 12, 2007 Share Posted August 12, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64532-search-box-help-needed/ Share on other sites More sharing options...
Snatch Posted August 12, 2007 Author Share Posted August 12, 2007 Anyone?? Quote Link to comment https://forums.phpfreaks.com/topic/64532-search-box-help-needed/#findComment-321767 Share on other sites More sharing options...
GingerRobot Posted August 12, 2007 Share Posted August 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64532-search-box-help-needed/#findComment-321773 Share on other sites More sharing options...
Snatch Posted August 12, 2007 Author Share Posted August 12, 2007 Thanks very much ginger! That works nicely! Quote Link to comment https://forums.phpfreaks.com/topic/64532-search-box-help-needed/#findComment-321811 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.