0

I have a html form where there are two Input fields say Caste (options are SC, ST and OBC) and Direction (options are north, south, east and west). Users will choose the options using check box.

<input name="ct" type="checkbox"  value="SC">SC
<input name="ct" type="checkbox"  value="ST">ST
<input name="ct" type="checkbox"  value="OBC">OBC

<input name="dr" type="checkbox"  value="N">North
<input name="dr" type="checkbox"  value="S">south
<input name="dr" type="checkbox"  value="E">East
<input name="dr" type="checkbox"  value="W">West

I have also a database (name : PEOPLE) with column name Caste , Direction etc.

Now I want to run a sql query based on user's selection. For example

mysql_query("select * from PEOPLE where Caste='option/options selected by user' and Direction= 'option/options selected by user' ")

If user choose one option from each field then it is not a problem for me,

mysql_query("select * from PEOPLE where Caste='$_POST[ct]' and Direction= '$_POST[dr]' ")

but if they use multiple options , then how should I proceed.

0

2 Answers 2

2

Give name attribute to the checkboxes like this

<input type="checkbox" name="caste[]" value="SC" />
<input type="checkbox" name="caste[]" value="ST" />
<input type="checkbox" name="caste[]" value="OBC" />

On you php side you can use function implode to form caste into a string as shown below (considering you are doing a POST)

$caste_id = implode(",",$_POST["caste"]);

Where you read from the database you can transform the value from db to an array like this

$caste_array = explode(",",$row->caste_id);

I hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

Please give me an example (mysql_query("select from ...........")) how to make query with $caste_array.
use foreach-loop for iterating the values from the $caste_array,inside loop write the sql query
1

You can use WHERE IN

$caste_string = implode('","', $_GET['ct']);
$caste_string = '"'.$caste.'"';

$dir_string = implode('","', $_GET['dr']);
$dir_string = '"'.$caste.'"';


mysql_query("select * from PEOPLE WHERE Caste IN ($caste_string) AND Direction IN ($dir_string)")

1 Comment

Please be aware that you have responded on a question from Sept 2014. I won't mind that only if the answer is in good quality. Yours isn't of good quality. Please use parameterized queries and stop using mysql_ functions. They're officially deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.