codelover
codelover

Reputation: 317

write sql from multiple checkbox

I researched a lot and found some solution ,probably not a convincing solution for me.Hence i am posting this question,Please help me

I Have A checkbox with same name and different values like

1.cate.php

<form action="mobile-phones-category.php" method="post">

<input type="checkbox" value="samsung" name="mobile[]"> sams

<input type="checkbox" value="nokia" name="mobile[]"> nolkia

<input type="submit" name="submit" value="SUBMIT" class="btn btn-primary pull-right">

</form>

2.) mobile-phones-category.php

I retrieve the values of check box on submit[array format] and want to search from db..I am using normal mysql_query(not pdos)

$search=$_POST["mobile"]; $search_string = implode(', ', $search); echo $search_string;

Here i Get something like Nokia,Sams

Next I write a single sql query include('connection.php');

$query = mysql_query("SELECT * FROM tablename where titles like '%$search_string%' ") or die(mysql_error());

What is happening is that only one value in the array is searched and not all the values in array..What changes should i Make so that all the array element should get searched

Thanks and regards

Upvotes: 0

Views: 143

Answers (2)

D. Schalla
D. Schalla

Reputation: 655

Like User016 said, I would also recommend using the IN Statement. It searchs for several searchterms, splitted by a ,.

You can find the Doc there: http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#function_in

Upvotes: 0

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

Use IN keyword in your query instead of LIKE

$query = mysql_query("SELECT * FROM tablename where titles IN ($search_string)" ) or die(mysql_error());

Usage Example:

$query = mysql_query("SELECT * FROM tablename where titles IN ('Nokia','Sams')" ) or die(mysql_error());

This will give you records with title Nokia & Sams from the table.

Upvotes: 1

Related Questions