简体   繁体   English

使用PHP将数组插入MySQL

[英]Insert Array into MySQL using PHP

So I have created a test that is all multiple choice.One of the questions is a has checkboxes. 因此,我创建了一个包含多项选择的测试。问题之一是具有复选框。

<br><br><b><div><legend><p style="margin-right:20px"><font color="red">*</font>
15.) Select your favorite colors (select all that apply)</legend></b><br><br>
<input type="checkbox" name="q15[]" id ="multi61" value="a" class="css-checkboxbox" ><label for="multi61" class="css-labelbox radGroup1"> Red </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="q15[]" id ="multi62" value="b" class="css-checkboxbox" ><label for="multi62" class="css-labelbox radGroup1"> Black </label><br><br>
<input type="checkbox" name="q15[]" id ="multi63" value="c" class="css-checkboxbox" ><label for="multi63" class="css-labelbox radGroup1"> Blue </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="q15[]" id ="multi64" value="d" class="css-checkboxbox" ><label for="multi64" class="css-labelbox radGroup1"> Green </label><br>
</p></div>

The problem that I'm having is that when you select multiple options, the values are added into an array. 我遇到的问题是,当您选择多个选项时,值将添加到数组中。 I want to add these values to one column. 我想将这些值添加到一列。 I know that I can't put a whole array into one column. 我知道我不能将整个数组放在一列中。 There are two options: 有两种选择:

  1. To add each item into its own column 将每个项目添加到自己的列中
  2. Put all values into one string 将所有值放入一个字符串

But I can't figure out how to do either. 但是我也不知道该怎么做。

If you are allowing user to select multiple answers, then it is fine with your string, goes into one column. 如果您允许用户选择多个答案,那么您的字符串就可以了,请放在一栏中。 This can be done in either two ways. 这可以通过两种方式完成。

  1. Serialize And UnSerialize array of multiple answers. SerializeUnSerialize Serialize多个答案的数组。
  2. JSON encoding/decoding of array of multiple answers. 多个答案数组的JSON编码/解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM