How to Save Multiple Check-boxes data to database?

喜你入骨 提交于 2020-01-15 15:29:14

问题


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.


回答1:


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.




回答2:


It is an extremely poor practice to store a delimited list of data in one column. It creates querying issues (and performance issues when you have to query using a nonsargable where clause). Create a separate table.



来源:https://stackoverflow.com/questions/9660778/how-to-save-multiple-check-boxes-data-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!