Page 1 of 1

PHP, MySQL & checkboxes

Posted: Mon Jun 09, 2003 6:52 am
by Lentildal
Hi all

+--------------+-------------------+
| Product_id | Product_image |
+--------------+-------------------+

I have a table with two columns as in the above diagram.

I have a page with 5 checkboxes like this:

<input type='checkbox' name='Prod_chk' value="<?= $array['Product_id']">

I want to run a query on my table to select the relevant products when a user checks some of the boxes but I'm stuck.

Is there some way of selecting the checked boxes something like (I know this syntax doesn't actually work):

Code: Select all

$temp_arr = array() //boxes that were checked 
 
"SELECT Product_image FROM My_products WHERE Product_id IN ($temp_arr)"

Thanks
L

Re: PHP, MySQL & checkboxes

Posted: Mon Jun 09, 2003 8:16 pm
by easyteck
Try something like this:

Code: Select all

<?php
$flag = true;
foreach ( $HTTP_POST_VARS as $key => $value )
{ // i.e: $key = 1_1_1_2 $value = true
  // you must have only checkboxes and a "submit" button
  // in your form 'cause this is just an example

  if ( $key != "submit" )   {
    if ($flag)
    {
      $temp = $key;
      $flag = false;
    }
    else
    {
      $temp .= ", " . $key;
    }
  }
}
// now you must have an string containing the ids selected in the form
// something like: "1, 4, 6, 10, 123, ...., 231"
// so you can use it directly in your SQL query!
$sSQL = "SELECT Product_image FROM My_products WHERE Product_id IN (" . $temp . ")"
?>
Cheers :idea:

Posted: Mon Jun 09, 2003 8:24 pm
by easyteck
Forgot to say:

Better use something like this:

Code: Select all

<input type='checkbox' name='Prod_chk<?php=$array['Product_id']?>' value="<?php=$array['Product_id']?>">
You got to have different name properties for each checkbox! so generate it with a generic prefix + the product_id...

Posted: Tue Jun 10, 2003 2:03 am
by Lentildal
Hey Easyteck - thanks for your reply, I managed to work it out in the end.

My checkbox names have [] on the end so they become an array.

The thing that was confusing me was the syntax for the speechmarks/commas inside the IN () part of the query.

Cheers
L