0

我有一个带有多选字段的表单。我为每个公司选择了 3 个类别,但只将一个添加到数据库中。

如何将 3 个类别的数组添加到我的数据库中?我使用连接表向公司添加多个类别。

我的表结构:

companies
---------
companyid
companyname
etc etc

categories
---------
categoryid
categoryname

companycategories
----------------
companycategoryid
categoryid
companyid

我的控制器:

function update()
{
    $id = $this->uri->segment(3);
    $data = array(
       'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
       'Postcode' => $this->input->post('Postcode'),
       'Plaats' => $this->input->post('Plaats'),
       'Telefoonnummer' => $this->input->post('Telefoonnummer'),
       'Email' => $this->input->post('Email'),
       'Website' => $this->input->post('Website'),
       'Profiel' => $this->input->post('Profiel'),
       'Adres' => $this->input->post('Adres'),
    );
    if($this->input->post('logo')) { $data['logo'] = $this->input->post('logo'); }
    $this->members_model->updatebedrijf($id, $data);

    $b = $this->session->userdata('idbedrijven');
    redirect("members/$b");
}   

我的模型:

function updatebedrijf($id, $data)
{

        $this->db->where('idbedrijven', $id);
        $this->db->update('bedrijven', $data); 

        $to_bedrijfcategorieen2['idcategorieen'] = $this->input->post('categorieen');
        $this->insert_bedrijfcat1($to_bedrijfcategorieen2); 

}

function insert_bedrijfcat1($data1) 
{ 
    echo '<pre>';
    print_r($data1);
    echo '</pre>';

    $id = $this->uri->segment(3); 
    $this->db->where('idbedrijven', $id);
    $this->db->update('bedrijfcategorieen', $data1); 

    return $this->db->affected_rows() >= 1 ? TRUE : FALSE; 
}

我的表格:

<tr>
<td><?= form_label('Categorieen'); ?></td>
<td><?= form_multiselect('categorieen[]', $opties, key($selectie)); ?></td>
</tr>

print_r($data1) 的输出;给了我这个:

Array
(
    [idcategorieen] => Array
        (
            [0] => 11
            [1] => 12
            [2] => 13
        )

)

希望很清楚。

4

1 回答 1

0

Update one to many relation这里是技术

 function insert_bedrijfcat1($data1) 
{     

$delete = $this->db->query("DELETE FROM `bedrijfcategorieen` WHERE `idbedrijven` = '" . $id. "'");

$id = $this->uri->segment(3); 

foreach($data1['idcategorieen'] as $cat_id){

$this->db->query("INSERT INTO `bedrijfcategorieen` (`idbedrijven`,`idcategorieen` ) VALUES ('".$id."','".$cat_id."')");

 }
 }
于 2013-05-30T12:03:40.240 回答