form_dropdown值未在提交codeigniter上更新

时间:2013-05-30 07:14:08

标签: php mysql forms codeigniter sql-update

场合

我有一个编辑表单用于编辑我的数据库中的公司。我使用连接表为公司添加类别。

我的桌子:

Companies
---------
idcompanies
companyname
country
telephone
etc...etc...

Categories
----------
idcategories
category

companycategories
-----------------
idcompanycategories
idcategories
idcompanies

问题

我的表单未更新下拉列表。可能是什么问题?

我的下拉表单代码:

<?php
    foreach($selected as $row){
        $selectie[$row->idcategorieen] = $row->Categorie;
    }
?>

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

我的更新控制器:

function updatebedrijven()
{
    $dbres = $this->db->get('categorieen');
    $ddmenu = array();
    foreach ($dbres->result_array() as $tablerow) {
        $ddmenu[$tablerow['idcategorieen']] = $tablerow['Categorie'];
    }
    $data['opties'] = $ddmenu;
    $id = $this->uri->segment(3); 
    $id2 = $this->uri->segment(3); 

    $data['selected'] = $this->members_model->getselection($id2);

    $data['info'] = $this->members_model->getbedrijf($id); 
    $data['id'] = $id;
    $this->load->view('members/header');
    $this->load->view('members/editform', $data);
    $this->load->view('members/footer');    
}

function update()
{
    $id = $this->uri->segment(3);
    echo 'id: '.$id;
    $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");
}   

注意:当我添加'Category'=&gt; $ this-&gt; input-&gt; post('categories')到数据数组我得到错误未知列。

我的模特:

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

    if($this->db->affected_rows() >= 1) 
    { 
    $to_bedrijfcategorieen2['idcategorieen'] = $this->input->post('categorieen');

    $this->insert_bedrijfcat1($to_bedrijfcategorieen2); 
    }else{ 
    return FALSE;
    } 
}

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

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

编辑:我发现它与我的$ selectie(选定值)有关。当我删除它有效。

2 个答案:

答案 0 :(得分:0)

您需要设置下拉列表的multiselect值,而不是key()使用set_multiselect('categorieen',$selectie) )

试试这个

 <td><?= form_dropdown('categorieen', $opties, set_multiselect('categorieen',$selectie)); ?></td>

供参考,请参阅this 希望它有所帮助

答案 1 :(得分:0)

事实证明,以下代码行是问题所在:

if($this->db->affected_rows() >= 1) 
{ 
$to_bedrijfcategorieen2['idcategorieen'] = $this->input->post('categorieen');

$this->insert_bedrijfcat1($to_bedrijfcategorieen2); 
}else{ 
return FALSE;
} 

必须是:

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

$this->insert_bedrijfcat1($to_bedrijfcategorieen2); 

我真的不知道为什么这给了我这个问题。但是通过删除它来修复它。