Rajneel Joshi
Rajneel Joshi

Reputation: 131

Insert multiple check box values in MYSQL database

i want to insert multiple selected checkbox values to be inserted in to one column, when i search for it, i found that append the values with comma and insert into table Example:

   ID      Value
-------------------
    1        Veg
    2        Non-Veg
    3        Chinese
    4        Italian

if user selects 1 3 4 then append with comma like 1,3,4 and insert into table. appending with comma is not the efficient way, Is there any other way ? please help me Thanks in advance

Upvotes: 1

Views: 2602

Answers (4)

Naveen Kumar
Naveen Kumar

Reputation: 4601

Assuming table would be like for order_id 33 tableA

order_id    item_id
33            1
33            3
33            4

you can get value by inner join above table and item, value table

(select * from tableA inner join tableB on tableA.item_id=tableB.item_id)

tableB

item_id      value
 1            Veg
 2            Non-Veg
 3            Chinese
 4            Italian

try this

$order_id=33;
$ids=$_GET['ids']; // $ids= 1,3,4
$ids_Split= explode(",", $ids);
$cnt=count($ids_Split);
for($i=0;$i<$cnt;$i++)
{
mysql_query("insert into hotel_table(order_id,item_id) 
values ($id, $ids_Split[$i])");
}

Upvotes: 1

Milan Halada
Milan Halada

Reputation: 1934

How about binary numbers for example:

0101

meaning

checkbox 1 : true
checkbox 2 : false
checkbox 3 : true
checkbox 4 : false

in db it would be stored like 5 (decimal value of binary 0101)

only problem would be if you would like to search users with for example checkbox 3 checked.

In that case just add row for every checkbox user checks. When searching for checkboxes you will get multiple rows with checkbox ids. I believe thats actually the best option.

Upvotes: 1

Daniel Gruszczyk
Daniel Gruszczyk

Reputation: 5612

Creating linked table where you can append multiple rows (1 for each selection) would be the first thing I would think of...

Upvotes: 0

juergen d
juergen d

Reputation: 204756

You should never put multiple values in one column which would violate the first narmal form of a database: http://en.wikipedia.org/wiki/First_normal_form

Just a column for every boolean field yo need.

Upvotes: 0

Related Questions