foreach($_POST['type'][0] as $checkbox){
echo $checkbox; #1, 2 or 3
$where_str = " AND gu.id_item='".$checkbox."' ";
}
$strSQL = "SELECT DISTINCT id FROM ".PROD_TABLE." gu WHERE gu.active='1' ".$where_str.";
$rs = $dbconn->Execute($strSQL);
When only one checkbox is checked, I got it working
but when 2 or more are checked, do not find any result.
You would be best off using IN in your WHERE clause.
This in combination of [fphp]array_map[/fphp], [fphp]intval[/fphp] and [fphp]sprintf[/fphp], you can build a pretty flexible and secure query builder.
<?php
error_reporting(-1);
ini_set('display_errors', true);
$sql = sprintf(
'SELECT id, name, email FROM table WHERE id IN (%s)',
implode(
',',
array_map(
'intval',
(array)$_POST['type'][0]
)
)
);
#SELECT id, name, email FROM table WHERE id IN (1,4,7)
?>
Be sure to read the docs. In brief, we ensure each of the checkboxes is an integer and build the where clause.
Great! it works perfectly.
One last thing: I need to use this in a sql query.
When I place this information into my WHERE condition, I get the last value …
and i need all values like “AND id=checkbox1 AND id=checkbox2”, etc
How to get them as differents variables? should I use “for” instruction?!
foreach($_POST['type'][0] as $checkbox){
echo $checkbox; #1, 2 or 3
$where_str = " AND gu.id_item='".$checkbox."' ";
}