Ok I am very new to the php mysql world so please bare with me.

I have set up a database for an apartment complex with a table containing the following:

create table apartments (apt_num int not null, available varchar(5) not null, beds int not null, baths int not null, pets varchar(5) not null, lease_start varchar(25), lease_end VARCHAR(25), descr varchar(25) not null, primary key (apt_num));

I am now creating a form that contains check boxes for users to select what they want to search for, and return the correct results... for example:

<input type=”checkbox” checked="checked" name=”bdrm[]” value=”Any″>
<input type=”checkbox” name=”bdrm[]” value=”1″>
<input type=”checkbox” name=”bdrm[]” value=”2″>
<input type=”checkbox” name=”bdrm[]” value=”3″>

<input type=”checkbox” checked="checked" name=”btrm[]” value=”Any″>
<input type=”checkbox” name=”btrm[]” value=”1″>
<input type=”checkbox” name=”btrm[]” value=”2″>

<input type=”checkbox” checked="checked" name=”pets[]” value=”Any″>
<input type=”checkbox” name=”pets[]” value=”Yes″>
<input type=”checkbox” name=”pets[]” value=”No″>

my question is: how would i use php to insert it into a query such as this:

SELECT * FROM apartments WHERE beds='bdrmChecked' AND baths='btrmChecked' AND pets='petsChecked';

Another problem is that if a user checked "Any" it wouldn't return any results because the value "Any" wouldn't exist in the table...

Any help is appreciated!

6 Years
Discussion Span
Last Post by vibhaJ

Check with below code.
This code will generate $sql based on your requirement.
you can modify as per your want.
if user have selected "Any" then no need to consider it as no filtration required for that in select query.

		$str = ' 1=1 ';		
		if( count($_POST['bdrm']) > 0 )
			$bdrm =  implode(',',$_POST['bdrm']); 	
			$str.= ' AND beds IN ('.$bdrm.')';
		if( count($_POST['btrm']) > 0 )
			$btrm =  implode(',',$_POST['btrm']); 	
			$str.= ' AND baths IN ('.$btrm.')';
		if( count($_POST['pets']) > 0 )
			$pets =  implode(',',$_POST['pets']); 
			$str.= ' AND pets IN ('.$pets.')';
		echo $sql = "SELECT * FROM apartments WHERE ".$str;		
<form name="form" id="form" method="post" action="">

<input type="checkbox" checked="checked" name="bdrmAny[]" value="Any">Any
<input type="checkbox" name="bdrm[]" value="1">1
<input type="checkbox" name="bdrm[]" value="2">2
<input type="checkbox" name="bdrm[]" value="3">3
<input type="checkbox" checked="checked" name="btrmAny[]" value="Any">Any
<input type="checkbox" name="btrm[]" value="1">1
<input type="checkbox" name="btrm[]" value="2">2
<input type="checkbox" checked="checked" name="petsAny[]" value="Any">Any
<input type="checkbox" name="pets[]" value="1">1
<input type="checkbox" name="pets[]" value="2">2

<input name="submit" value="Search" type="submit">


Thank you so much! This really helps out, except I am wondering one thing.... What happens if the the user checks "Any", "1", and "2"....This would in fact mean that you would want to return "Any". I'm unsure how I would modify this code to make that work... or is there a way to disable people to check multiple check boxes IF "Any" is checked? Sorry if that is a little confusing...I appreciate the help!


You can use javascript code that if user select any other option other than 'Any' then make js code will make 'Any' unchecked.
This way you can implement your requirement.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.