Click here to Skip to main content
15,313,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database table named with books. it consists of something like below

     title         |     author           |    category
                   |                      |  

    xyz                   madman                  fiction

    adapt                 brainless               fiction
    adaxx                   stupid                fiction

    follow                 idiot                  journal

    your                    man                   acadamic

user is trying to search the value by author or title and based on category
ex: if user search for adapt by selecting fiction need display both adapt and adaxx
below is the code
using mysql database
<form action="search.php" method="post">
$select_query=          "Select distinct category from books";
$select_query_run =     mysql_query($select_query);
$select_query_array=   mysql_fetch_array($select_query_run) ;
 $cat = $select_query_array['category'];
echo "<select name='name'>";
while ($select_query_array=   mysql_fetch_array($select_query_run) )
{// dynamic drop list

  echo "<option value='".htmlspecialchars($select_query_array["category"])."' >".htmlspecialchars($select_query_array["category"])."</option>";

echo "</select>";
   <input type="text" name="searchvalue" size="5"  id="searchfield" title="searchfield" onFocus="clearText(this)"/>
     <input type="submit" name="submit" value="" alt="Search" id="searchbutton" title="Search" />
$button = $_POST['submit'];
$search = $_POST['searchvalue']; 
$name = $_POST['name'];
//echo $a;
echo "Search term too short";
echo "You searched for $search <hr size='1'></br>";
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";  
foreach($search_exploded as $search_each)
$construct .="title LIKE '%$search_each%'";
$construct .="AND title LIKE '%$search_each%'";
$construct ="SELECT * FROM books WHERE $construct and category = '$name'";// I tried this but not working

$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for $search.</br></br>1.";
echo "$foundnum results found !<p>";
Updated 20-Feb-15 8:02am
PIEBALDconsult 20-Feb-15 13:29pm
Which database system? Should we assume MySQL? Please use Improve question to add detail and context.
Member 11406049 20-Feb-15 13:33pm
yes mysql
W Balboos, GHB 20-Feb-15 13:51pm
SQL: You'd probably be better served if your category column was of type int and just had the numerical values - There's no value to have 'cat x' when every entry begins with 'cat '.

PHP: what do expect to ever happen to the value you have in $button ?

Considering your previous question, this is starting to seem too much like homework.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900