PHP, MySQL & checkboxes

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Lentildal
Forum Newbie
Posts: 2
Joined: Mon Jun 09, 2003 6:52 am

PHP, MySQL & checkboxes

Post 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
easyteck
Forum Newbie
Posts: 13
Joined: Thu Jun 05, 2003 12:01 pm
Location: Ecuador
Contact:

Re: PHP, MySQL & checkboxes

Post 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:
easyteck
Forum Newbie
Posts: 13
Joined: Thu Jun 05, 2003 12:01 pm
Location: Ecuador
Contact:

Post 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...
Lentildal
Forum Newbie
Posts: 2
Joined: Mon Jun 09, 2003 6:52 am

Post 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
Post Reply