如何在mysql codeigniter中插入多个复选框值作为连续行

时间:2015-09-04 05:49:12

标签: php mysql codeigniter checkbox

employer_rgn.php

<div class="form-group">
  <label for="" class="control-label col-xs-2">
    <?php echo $this->lang->line('spclzd_ctgry'); ?>
  </label>
  <div class=" col-md-8">            
    <input type="checkbox" name="spec_cat[]" value="Information Technology"> Information Technology 
    <input type="checkbox" name="spec_cat[]" value="Engineering / Manufacturing">Engineering / Manufacturing <br />
    <input type="checkbox" name="spec_cat[]" value="Banking & Financial Services"> Banking & Financial Services  
    <input type="checkbox" name="spec_cat[]" value="BPO / ITES"> BPO / ITES <br />
    <input type="checkbox" name="spec_cat[]" value="FMCG / Retail"> FMCG / Retail  
    <input type="checkbox" name="spec_cat[]" value="Telecom / ISP"> Telecom / ISP <br />
    <input type="checkbox" name="spec_cat[]" value="Pharmaceuticals / Health Care"> Pharmaceuticals / Health Care  
    <input type="checkbox" name="spec_cat[]" value="Sales & Marketing"> Sales & Marketing <br />
    <input type="checkbox" name="spec_cat[]" value="Other Non IT"> Other Non IT
  </div>
</div>

employer_model

public function insert()
{
  //Insert second stage details for employer into database.
  $Specilized_category = $this->input->post('spec_cat');
  $data=array('Specilized_category'=>implode(",", $Specilized_category),);
  $this->db->insert('tbl_employer', $data);

我将复选框值插入数据库,用逗号分隔。但问题是,我需要从数据库中取回已检查的值以编辑我的代码。我怎么能得到这些值,因为我使用了implode方法?或者我是否必须更改用于将值插入数据库的代码?

2 个答案:

答案 0 :(得分:2)

当你内爆它创建一个数组,所以当它试图插入到mysql时它的类型可以是数组。但转换成字符串或json字符串后,您可以存储多个值。

喜欢......

public function insert()
{
    //Insert second stage details for employer into database.
    $Specilized_category = $this->input->post('spec_cat');
    $data=array(
    'Specilized_category'=>json_encode(implode(",", $Specilized_category)),
);
$this->db->insert('tbl_employer', $data);

试一试,如果您在插入时遇到任何问题,请告诉我们。['}

答案 1 :(得分:0)

此模型给出以下错误

发生数据库错误

错误号码:1452

无法添加或更新子行:外键约束失败(shubhttc_onlinedestination_route,CONSTRAINT destination_route_ibfk_2 FOREIGN KEY(to_id)参考departure_citydep_id))

INSERT INTO destination_routeto_id)VALUES(&#39; \&#34; 2,4,5 \&#34;&#39;)

文件名:C:/xampp/htdocs/shubhttc_online/system/database/DB_driver.php