【问题标题】:Handling multiple checkbox array in codeigniter在codeigniter中处理多个复选框数组
【发布时间】: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-&gt;input-&gt;post("checkbox_name");。因此,我通过在运行时创建表单所需的数据库字段,直接将表单数据插入数据库。并通过以下方式插入表单数据:

 $this->input->post();   //directly to model.

但是,当我尝试以相同的方式插入多个复选框数组时,它会在 codeigniter 库的 mysql/mysql_driver.php 中引发错误 Array to string conversion。我该如何克服这个问题。请帮忙。

更新

我从$this-&gt;input-&gt;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-&gt;input-&gt;post(); print_r($postdata);
  • 这给了我所有带有键值对的表单数据的数组。如果多个复选框它在表单数据数组中创建另一个子数组。
  • 然后发布该数组,在这里您将获得 checkbox 的名称 attr
  • 它是来自数据库的动态,所以我不知道运行时哪个是复选框。所以我不能这样做。
  • 发布你在$postdata变量中得到的数组

标签: php mysql forms codeigniter


【解决方案1】:

如果您想以这种方式存储爱好,则应始终使用带有外键的单独表与您的用户表相关联。表示您的爱好表包含用户的爱好。即使用用户 ID,tbl_hobbies.id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多