Re: Building A Query
| From: | John W. Holmes | Date: | Wed, 11 Feb 2004 15:31:16 +0000 |
| Subject: | Re: Building A Query | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-32902@lists.php.net to get a copy of this message | ||
From: "Benjamin Jeeves" <ben@jeeves92.freeserve.co.uk>
> I have a form with a number of checkboxs on it and
> what I would like to be able to do is build a query
> base on the checked checkedboxes.
All depends on what you're actually doing, but I assume each checkbox is
related to a row in the database? And each row as a unique identifier (like
an auto_increment number)?
If you name you checkboxes such as "box[]" and put the unique identifier as
the value, it's easy to build a list of checkboxes that were checked.
<input type="checkbox" name="box[]" value="1">
<input type="checkbox" name="box[]" value="2">
<input type="checkbox" name="box[]" value="3">
You can now build a query such as:
<?php
if(isset($_POST['box']) AND is_array($_POST['box']) &&
!empty($_POST['box']))
{ $query = 'SELECT * FROM table WHERE id IN (' .
implode(',',$_POST['box'])
. ')'; }
You may want to actually loop through $_POST['box'], though, and validate
each value as a number, string, etc, before you stick then in your query.
---John Holmes...