我有一个多选字段的表单。我为每家公司选择3个类别,但只有一个被添加到数据库中。如何在Codeigniter中将数组添加到数据库中?
如何将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($数据1)的输出;给我这个:
Array
(
[idcategorieen] => Array
(
[0] => 11
[1] => 12
[2] => 13
)
)
希望它是明确的。
,companyid相同,但类别ID不同。在那里添加3个类别。然后你可以通过一个简单的查询来获得它,例如'SELECT * FROM'companycategories' WHERE'companyid' = x – itachi
这就是我正在做的。但我无法让它工作。 –