du656637962 2016-05-07 10:44
浏览 491

如何将复选框值存储到mysql字段进行搜索?

I want to storage checkbox valus to a field.

html like this

two value selected

<input type="checkbox" value="1">invest_type1
<input type="checkbox" value="2" checked>invest_type2
<input type="checkbox" value="3" checked>invest_type3
<!-- more, but less than 20-->

mysql table dict

id
project_name
invest_types   // how to save?
// other fields

Demand

multiple select

exp. User select "invest1" and "invest2",then how to fetch that row?

Plans

  1. use bit method to storage.

    invest_type2 is 2 , pow(2,2) => 4

    invest_type2 is 3 , pow(2,3) => 8

    4+8=12,when select use invest_types & (invest_type1's pow + invest_type2's pow)

  2. add table to relate

    add table to storage invest_types with every project.

    use join and distinct when use search.

Problem & Help

  • Which plan is the best?
  • What about efficiency of plan1?
  • 写回答

1条回答 默认 最新

  • dongmangwei3822 2016-05-07 11:00
    关注

    Which plan is the best?

    It depends on your ER model. If you only need to store and search trough those preferences I will go with plan1. Instead if you need to associate those preferences to multiple related entities I will go with plan2

    What about efficiency of plan1?

    Your proposed plan1 can be very efficient, in fact you can use php bitwise functions (http://php.net/manual/en/language.operators.bitwise.php) to create the values you will save into the db and then use the mysql bitwise functions to retrieve and search on it (http://dev.mysql.com/doc/refman/5.7/en/bit-functions.html)

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办