如何将多个复选框数据保存到数据库? [英] How to Save Multiple Check-boxes data to database?

查看:268
本文介绍了如何将多个复选框数据保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个专家,所以我真的很感激,如果你真正具体的你的答案。



我有这个注册表单,复选框,我想知道什么最好的方法是将其保存在数据库中。我不知道所有的值是否应该转到一个单独的列,或者如果我应该为我的注册表单的这一部分创建一个不同的表。同样重要的是要考虑到,我以后需要从数据库中提取所有这些数据,并在管理后台中显示它,它将用于更新数据库的版本。下面您可以看到包含复选框的部分的html代码。

 < p&只有您感兴趣的产品。(季度的预计购买金额。)< / p> 
< table cellpadding =15>
< tbody>
< tr>
< th>手提包< / th>
< th>时尚饰品< / th>
< th>手表< / th>
< th>水晶旅行首饰< / th>
< th>高级珠宝< / th>
< / tr>
< tr>
< td>
< input type =checkboxname =pro_amount []value =1/> $ 2500 +< / br>
< input type =checkboxname =pro_amount []value =2/> $ 1000- $ 2500< / br>
< input type =checkboxname =pro_amount []value =3/>高达$ 1000< / br>
< / td>
< td>
< input type =checkboxname =pro_amount []value =4/> $ 2500 +< / br>
< input type =checkboxname =pro_amount []value =5/> $ 1000- $ 2500< / br>
< input type =checkboxname =pro_amount []value =6/>高达$ 1000< / br>
< td>
< input type =checkboxname =pro_amount []value =7/> $ 2500 +< / br>
< input type =checkboxname =pro_amount []value =8/> $ 1000- $ 2500< / br>
< input type =checkboxname =pro_amount []value =9/>最高$ 1000< / br>
< td>
< input type =checkboxname =pro_amount []value =10/> $ 2500 +< / br>
< input type =checkboxname =pro_amount []value =11/> $ 1000- $ 2500< / br>
< input type =checkboxname =pro_amount []value =12/>高达$ 1000< / br>
< td>
< input type =checkboxname =pro_amount []value =13/> $ 2500 +< / br>
< input type =checkboxname =pro_amount []value =14/> $ 1000- $ 2500< / br>
< input type =checkboxname =pro_amount []value =15/>高达$ 1000< / br>
< / tr>

< / tbody>
< / table>
< p> 5)以下每个项目的平均零售价是多少? (仅选中适用的选项。)< / p>
< table cellpadding =15>
< tbody>
< tr>
< th>手提包< / th>
< th> Jewelry< / th>
< th>手表< / th>
< / tr>
< tr>
< td>
< input type =checkboxname =av_rtp []value =1/> $ 125或更高< br>
< input type =checkboxname =av_rtp []value =2/> $ 75< / br>
< input type =checkboxname =av_rtp []value =3/> $ 40< / br>
< / td>
< td>
< input type =checkboxname =av_rtp []value =4/> $ 125或更高< br>
< input type =checkboxname =av_rtp []value =5/> $ 75< / br>
< input type =checkboxname =av_rtp []value =6/> $ 40< / br>
< td>
< input type =checkboxname =av_rtp []value =7/> $ 125或更高< br>
< input type =checkboxname =av_rtp []value =8/> $ 75< / br>
< input type =checkboxname =av_rtp []value =9/> $ 40< / br>
< / tr>
< / tbody>
< / table>



非常感谢您提供任何帮助。


<

您有几个选项...

1)允许每个类别检查一个框:

这种情况下,更好的是,如果您将复选框更改为单选按钮,这样更容易一次只保留一个选中,您只需要存储在数据库的一列中选择的一个按钮的值。 br />

2)允许每个类别检查多个框:

在这种情况下,您可以保留复选框,并建议将复选框的总值一列保持您的数据库的负载。但是,这意味着您需要包括某种形式的脚本,反向添加您在将信息存储在数据库中之前。您可以通过使每个类别中的三个复选框的值为1,10和100来实现此目的,以便您可以通过使用一系列%操作轻松检查哪些复选框已选中。


< table cellpadding =15>

< tbody>

< tr>

< th>手袋< / th> code>

< th> Jewelry< / th>

; th> Watches< / th>

< / tr>

< tr>

< td>

< br /> < input type =checkboxname =av_rtp [] 1value =1/> $ 125或更高$ b < input type =checkboxname =av_rtp [] 1value =10/> $ 75< br />

< input type =checkboxname =av_rtp [] 1value =100/> $ 40< br />

< / td>

< td>

< input type =checkboxname =av_rtp [] 2value =1 > $ 125或更高版本< br />

< input type =checkboxname =av_rtp [] 2value = 10/> $ 75< br />

< input type =checkboxname =av_rtp [] 2 =100/> $ 40< br />

< td>

< input type =checkboxname =av_rtp [] 3value =1/> $ 125或更高< br />

< input type =checkboxname =av_rtp [] 3value =10/> $ 75< br />

< input type =checkboxname =av_rtp [] 3value =100/> $ 40< br />

< / tr>

< / tbody>

< / table>

*注意每个组有不同的名称,以保持它们分开。无论您使用单选按钮还是复选框,都应该这样做。

让我知道这是否有帮助。


I am not an expert so I'd really appreciate if you are real specific on your answers.

I have this registration form that has a section with lots of check-boxes, and I wondering what the best way is to save this in the database. I am not sure if all values should go to a single column, or If I should create a different table only for this section of my registration form. It's also important to take into account that I will later need to pull all this data from the database and to show it in the "admin back-end" where it would be available for edition to update the database. Below you can see part of the html code for the section containing the check-boxes.

<p>4) Please select only the product(s) you are interested in. (Anticipated purchase amounts for the quarter.)</p>
        <table  cellpadding="15" >
         <tbody>
    <tr>
      <th>Handbags</th>
      <th>Fashion Jewelry</th>
      <th>Watches</th>
      <th>Crystal Travel Jewelry</th>
      <th>Fine Jewelry</th>
    </tr>
<tr>
<td>
<input type="checkbox" name="pro_amount[]" value="1" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="2"  />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="3"  />Up to $1000 </br>    
</td>
  <td>
<input type="checkbox" name="pro_amount[]" value="4" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="5"  />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="6"  />Up to $1000 </br>  
<td>
<input type="checkbox" name="pro_amount[]" value="7" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="8"  />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="9"  />Up to $1000 </br>  
<td>
<input type="checkbox" name="pro_amount[]" value="10" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="11"  />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="12"  />Up to $1000 </br>  
<td>
<input type="checkbox" name="pro_amount[]" value="13" />$2500+ </br>
<input type="checkbox" name="pro_amount[]" value="14"  />$1000-$2500 </br>
<input type="checkbox" name="pro_amount[]" value="15"  />Up to $1000 </br>        
</tr>

  </tbody>
        </table>
        <p>5) What are the average retail price points for each of the following items? (Check only those that apply.)</p>
        <table  cellpadding="15" >
         <tbody>
    <tr>
      <th>Handbags</th>
      <th>Jewelry</th>
      <th>Watches</th>
    </tr>
<tr>
<td>
<input type="checkbox" name="av_rtp[]" value="1" />$125 or greater<br>
<input type="checkbox" name="av_rtp[]" value="2"  />$75</br>
<input type="checkbox" name="av_rtp[]" value="3"  /> $40</br>    
</td>
  <td>
<input type="checkbox" name="av_rtp[]" value="4" />$125 or greater<br>
<input type="checkbox" name="av_rtp[]" value="5"  />$75</br>
<input type="checkbox" name="av_rtp[]" value="6"  /> $40</br>    
<td>
<input type="checkbox" name="av_rtp[]" value="7" />$125 or greater<br>
<input type="checkbox" name="av_rtp[]" value="8"  />$75</br>
<input type="checkbox" name="av_rtp[]" value="9"  /> $40</br>     
</tr>
      </tbody>
        </table>

Jsfiddle Demo if that helps.!

Thanks a lot in advance for any help you can provide.

解决方案

You have a couple options…
1)Allowing one box to be checked per category:
In this case, it would be better if you changed your checkboxes to radio buttons that way it is easier to keep only one checked at one time and you will only have to store the value of the one button selected in one column of your database.

2)Allowing multiple boxes to be checked per category:
In this case, you can keep your checkboxes and would suggest storing the total value of the checkboxes in one column to keep the load on your database down. However, this means that you need to include some form of script reverse the addition you did before storing the info in your database. You could do this by having the values of the three check boxes in each category be 1, 10 and 100 so that you could easily check which ones were checked by using a series of % operations.

<table cellpadding="15" >
<tbody>
<tr>
<th>Handbags</th>
<th>Jewelry</th>
<th>Watches</th>
</tr>
<tr>
<td>
<input type="checkbox" name="av_rtp[]1" value="1" />$125 or greater<br />
<input type="checkbox" name="av_rtp[]1" value="10" />$75<br />
<input type="checkbox" name="av_rtp[]1" value="100" /> $40<br />
</td>
<td>
<input type="checkbox" name="av_rtp[]2" value="1" />$125 or greater<br />
<input type="checkbox" name="av_rtp[]2" value="10" />$75<br />
<input type="checkbox" name="av_rtp[]2" value="100" /> $40<br />
<td>
<input type="checkbox" name="av_rtp[]3" value="1" />$125 or greater<br />
<input type="checkbox" name="av_rtp[]3" value="10" />$75<br />
<input type="checkbox" name="av_rtp[]3" value="100" /> $40<br />
</tr>
</tbody>
</table>
*Notice how each group has a different name, to keep them separated. You should do this whether you use radio buttons or checkboxes since they mean different things.
Let me know if this helps or not.

这篇关于如何将多个复选框数据保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆