我要构建一个调查表生成器。表单中的 name
属性是动态的,来自数据库。所以我不知道任何输入元素的 name
。从数据库中,我已经生成了表单及其工作,但是我在提交多个复选框值并将其存储到数据库中时遇到了问题。
我试过了:
查看
// $inputtype, $name comes from database.
<input type="$inputtype" name="$name"[]>
// I can fetch values of all checked option and loop through it and convert it into string using explode and insert into database. Thats not a big deal.
但是,问题在于 name
属性是数据库中的动态属性。我在运行时不知道 name
属性。所以我不能做 $this->input->post("checkbox_name");
。因此,我通过创建运行时表单所需的数据库字段,直接将表单数据插入数据库。并通过执行以下操作插入表单数据:
$this->input->post(); //directly to model.
但是,当我尝试以相同方式插入多个复选框数组时,它会在 codeigniter 库的 mysql/mysql_driver.php
中抛出错误 Array to string conversion
。我怎样才能克服这个问题。请帮忙。
更新
我从 $this->input->post()
提交表单后得到的数组
array
'first_name' => string 'sushil ' (length=7)
'last_name' => string 'shrestha' (length=8)
'gen' => string 'Male' (length=4)
'hobbies' =>
array
0 => string 'gaming' (length=6)
1 => string 'football' (length=8)
2 => string 'cricket' (length=7)
'language' => string 'IOS' (length=3)
但我不能将爱好数组直接存储到数据库中。
最佳答案
如果你想以这种方式存储爱好,你应该始终使用单独的表,外键与你的用户表相关联。意味着您的爱好表包含用户的爱好。即使用用户 ID,tbl_hobbies.id
关于php - 在 codeigniter 中处理多个复选框数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31377130/