search a mysql database using checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

search a mysql database using checkboxes

Post by leewad »

Hi

I`m wanting to have a refined search where you can have different checkboxes on a form for fields to search from i e

search for a car less than £500, aircon, leather, CD using checkboxes could anyone help me with this? I have the checkboxes in an array but not sure how to pull the info from the database.

Thanks
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

What you are aiming to achieve is to build a (SQL) Statement to pass to the Database.

Essentially a string.

So you would have sub-strings stored which you would include if the relevant checkbox is checked.

A very brief example, for the receiving page:

Code: Select all

<?php

$sql = "SELECT * FROM cars"

if (isset($_POST['chkbox1']) {

$sql .= " WHERE airCon = 1";

}

mysql_query($sql);

?>
Post Reply