开发者

How to use a PHP checkbox form to SELECT from MySQL datatype SET (multiple choice) column values?

I have a MySQL database with a column of datatype SET (multiple selection, say apples, oranges, grapes)

I want to have a form with checkboxes for those values (apples, oranges, grapes etc) so one can select a breakfast basket type that contains for example, oranges and grapes.

It would pull from the database those baskets with those selection of fruits (specified in the column 'Fruits', which is a EM datatype column. (I have a multiple selection when entering the values for the column.)

Can it be done? Should I make another table with FruitNames?

Also, when I add a new value to the multiple selection list, all my previous entries disappear and I have to re-enter all the values again... Please help!

  • How to search for those entries that match the multiple selection (from the SET type column)?
  • Can I edit the list of val开发者_JAVA技巧ues in the SET type column without erasing previous entries?

Thanks in advance.


Yes, you will need another table. That is called database normalization.

Can you show me query that adds values to list?

I will give you PHP code how to implement selection of fruits.

$fruits = implode(',', $_POST['fruits']);
mysql_query('SELECT name, fruit_id FROM fruits WHERE fruit_id IN ('.$fruits.')');

Here is the form

<input type="checkbox" name="fruits[1]"> Apple<br>
<input type="checkbox" name="fruits[2]"> Pear<br>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜