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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.