复选框怎么保存到数据库和从数据库中读出来?

WBOY
Release: 2016-06-06 20:18:59
Original
2415 people have browsed it

复选框怎么保存到数据库和从数据库中读出来?用php和mysql。

<code><label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox1" value="option1"> 1
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox2" value="option2"> 2
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox3" value="option3"> 3
</label></code>
Copy after login
Copy after login

回复内容:

复选框怎么保存到数据库和从数据库中读出来?用php和mysql。

<code><label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox1" value="option1"> 1
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox2" value="option2"> 2
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox3" value="option3"> 3
</label></code>
Copy after login
Copy after login

方法1,用Form表单
使用相同的name 后面跟中括号,例如 inlineCheckbox[]
后端获取inlineCheckbox 为数组
方法2,自定义post请求
用jQuery,获取所有value,组装成一个值,post后端,自己解析

不太清楚楼主想表达的,先按照自己理解回答吧.
假设有一个问题" 您的爱好 ",多选,选择了篮球,足球,台球三项,那么数据库里可能对应一个叫做hobby的字段,这个字段的值就是这三个答案,这三个答案依照规定的链接符链接,比如" 篮球|足球|台球 ".
那么在读取的时候,首先使用explode将答案拆解成一个数组,循环数组,一项一项对比答案,两个值一样了,说明这个答案就被选择了,给这个答案赋值html属性checked,就可以了.

一个比较原始简单粗暴的多选问题就解决了.

  • 在一个column存多个值(json字符串或者你再发明个编码). 注意这个是违反1NF的, 不过坏处有限.

  • 另开个表, 用外键

  • 开多个column, 各存一个值

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!