【发布时间】:2015-10-01 08:22:51
【问题描述】:
我将构建一个调查表生成器。 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 attr 在运行时。所以我不能做$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)
但我不能将爱好数组直接存储到数据库中。
【问题讨论】:
-
试试
$postdata = $this->input->post(); print_r($postdata); -
这给了我所有带有键值对的表单数据的数组。如果多个复选框它在表单数据数组中创建另一个子数组。
-
然后发布该数组,在这里您将获得
checkbox的名称 attr -
它是来自数据库的动态,所以我不知道运行时哪个是复选框。所以我不能这样做。
-
发布你在
$postdata变量中得到的数组
标签: php mysql forms codeigniter