Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8394289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:01:17+00:00 2026-06-09T20:01:17+00:00

I’m trying with search box, I created a form as below: <form method="post" action="search.php">

  • 0

I’m trying with search box, I created a form as below:

<form method="post" action="search.php">
 <select name="purpose">
 <option value="" selected="selected">-any-</option>
 <option value="For Sale">For Sale</option>
 <option value="For Rent">For Rent</option>
 </select>

 <select name="type">
 <option value="" selected="selected">-any-</option>
 <option value="Bungalow">Bungalow</option>
 <option value="Apartment">Apartment</option>
 </select>

 <select name="location">
 <option value="" selected="selected">-any-</option>
 <option value="Norway">Norway</option>
 <option value="Itley">Itley</option>
    </select>
 <input type="submit" value="Search">
</form>

Search.php

I’m trying with these queries but getting problem:

$purpose=$_POST['purpose'];
$type=$_POST['type'];
$location=$_POST['location'];

If I put AND query like this:

SELECT * FROM test WHERE purpose='$purpose' AND location='$location' AND type='$type'

then it not filter one by one result its appear blank.

If I put OR query like this:

SELECT * FROM test WHERE purpose='$purpose' OR location='$location' OR type='$type'

Then it filter mix results.

I want if all selected it filter (purpose >> type >> location) AND if one selected then filter by this but exact result else show

Result not found!

EDIT Adding update made in comments:

I’m doing like this but it showing error:

$qry = "SELECT * FROM test WHERE 1=1";

if($purpose!="")
  $qry .= " AND purpose='$purpose'";

if($location!="")
  $qry .= " AND location='$location'";

if($type!="")
  $qry .= " AND type='$type'";

while ($row = mysql_fetch_array($sql)) 

echo $row['purpose']; 

echo $row['location']; 

echo $row['type']; 

I want if not match display result not found else it filter by all and one by one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:01:18+00:00Added an answer on June 9, 2026 at 8:01 pm

    Put that after obtaining the variables:

    $sql = array();
    
    if (!empty($purpose)) {
        $sql[] = "purpose='$purpose'";
    }
    
    if (!empty($type)) {
        $sql[] = "type='$type'";
    }
    
    if (!empty($location)) {
        $sql[] = "location='$location'";
    }
    
    $sql = implode(' AND ', $sql);
    $sql = "SELECT * FROM test" . (!empty($sql)?: " WHERE " . $sql: '');
    

    EDIT: After seeing your comments

    First, you should make the connection to the database with the mysql_connect functions. Let’s say, for example, that you have this piece of code:

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    

    In the $link var now you have your mysql handle. This piece of code must go at the first of every page where you wanna use the mysql connection. Once you have the SQL sentence (in the code I first wrote, the $sql var), use this after:

    $result = mysql_query($sql);
    
    if (mysql_num_rows($result) === 0) {
        echo 'Result not found';
    }
    
    while ($row = mysql_fetch_array($result)) {
       echo $row['purpose'] . '<br/>';
       echo $row['location'] . '<br/>';
       echo $row['type'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.