Jump to content

Mutiple options for search box


Alicia

Recommended Posts

Hi I am trying to create a search box with 5 optional textfield and can you give me a idea why my form and search query is not working ? in the submitted form, my php mysql query as below that returns either not accurate result or nothing found. =(

 

 

My form

<form name="form1" method="post" action="wheretofind.php">
<table cellpadding="5" cellspacing="0" border="0" height="100">
<tr>
  <td>Company Name </td>
	<td> :</td>
  <td><input name="keyword" type="text" value="Any"/></td>
</tr>
<tr>
  <td>Occupation</td>
	<td> :</td>
	<td>
		<select name="professional">
		  <option value="Any" selected="selected">Any</option>
			<option value="Engineer">Engineer</option>
			<option value="Technician">Technician</option>

			<option value="Other">Other</option>
        </select>		</td>
</tr>
<tr>
  <td>City</td>
  <td> </td>
  <td><input name="city" type="text" value="Any" onkeypress="return submitenter(this,event)" onclick="this.value='';" id="city" /></td>
    </tr>
<tr>
  <td>State</td>
  <td> </td>
  <td><input name="state" type="text" value="Any" onkeypress="return submitenter(this,event)" onclick="this.value='';" id="state" /></td>
    </tr>
<tr>
  <td>Country</td>
	<td>: </td>
	<td>
		<select name="country">
		  <option value="Any" selected="selected">Any</option>
			<option value="AF">Afghanistan</option>
			<option value="AL">Albania</option>
			<option value="DZ">Algeria</option>
			<option value="AS">American Samoa</option>
	  </select>		</td>
</tr>
<tr>
	<td colspan="3"><input type="submit" name="submit" value="Search" /></td>
</tr>
</table>
</form>

 

 

submitted form php mysql script


  $search_query = 'mysql_query("select * from Students where';
          
          if(($keyword !='Any') || ($keyword !=''))
          	{ 
            $search_query .="NickName LIKE \'%$keyword%\' or DescriptionMe Like \'%$keyword%\'";
            }
            
           if(($professional !='Any') || ($professional !=''))
          	{ 
            $search_query .="AND Current_Pro = \'$profession\'";
            }
                 if(($city !='Any') || ($city !=''))
          	{ 
            $search_query .="AND City = \'$city\'";
            }
     	  if(($state !='Any') || ($state !=''))
          	{ 
            $search_query .="AND State = \'$state\'";
            }
          if($country !='Any')
          	{ 
            $search_query .="AND Country = \'$country\'";
            }
            
       $search_query .= 'ORDER BY `Students`.`NickName` ASC ") or die(mysql_error())';

 

Please advise and thanks.

Link to comment
https://forums.phpfreaks.com/topic/144935-mutiple-options-for-search-box/
Share on other sites

You do realize you cannot put a function in a string and expect it to be called.

 

Try this:

 


  $search_query = 'select * from Students where';
         
          if(($keyword !='Any') || ($keyword !=''))
             {
            $search_query .="NickName LIKE \'%$keyword%\' or DescriptionMe Like \'%$keyword%\'";
            }
           
           if(($professional !='Any') || ($professional !=''))
             {
            $search_query .="AND Current_Pro = \'$profession\'";
            }
                 if(($city !='Any') || ($city !=''))
             {
            $search_query .="AND City = \'$city\'";
            }
          if(($state !='Any') || ($state !=''))
             {
            $search_query .="AND State = \'$state\'";
            }
          if($country !='Any')
             {
            $search_query .="AND Country = \'$country\'";
            }
           
       $search_query .= 'ORDER BY `Students`.`NickName` ASC';
      $result = mysql_query($search_query) or die(mysql_error());
     while ($row = mysql_fetch_assoc($result)) {
            print_r($row);
    }

 

And see if that works for you.

Not working..

 

This is the error :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE \'%key%\' or DescriptionMe Like \'%key%\'AND Occupation = \'\'AND City = \' at line 1

Try:


  $search_query = 'select * from Students where ';
         
          if(($keyword !='Any') || ($keyword !=''))
             {
            $search_query .="NickName LIKE \'%$keyword%\' or DescriptionMe Like \'%$keyword%\' ";
            }
           
           if(($professional !='Any') || ($professional !=''))
             {
            $search_query .="AND Current_Pro = \'$profession\' ";
            }
                 if(($city !='Any') || ($city !=''))
             {
            $search_query .="AND City = \'$city\' ";
            }
          if(($state !='Any') || ($state !=''))
             {
            $search_query .="AND State = \'$state\' ";
            }
          if($country !='Any')
             {
            $search_query .="AND Country = \'$country\' ";
            }
           
       $search_query .= 'ORDER BY `Students`.`NickName` ASC';
      $result = mysql_query($search_query) or die(mysql_error());
     while ($row = mysql_fetch_assoc($result)) {
            print_r($row);
    }

All I did was add spaces in, I think your query looked like this

select * from Students whereNickName LIKE ........

where and NickName got bunched cuz you didn't have a space.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'%key%\' or DescriptionMe Like \'%key%\' AND Current_Pro = \'\' AND City = \'\'' at line 1

select * from Students where NickName LIKE \'%key%\' or DescriptionMe Like \'%key%\' AND Current_Pro = \'\' AND City = \'\' AND State = \'\' AND Country = \'\'ORDER BY `Students`.`NickName` ASC

Archived

This topic is now archived and is closed to further replies.

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