【问题标题】:form_dropdown value not updating on submit codeigniterform_dropdown 值未在提交 codeigniter 时更新
【发布时间】:2013-05-25 16:03:36
【问题描述】:

情况

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

我的桌子:

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' => $this->input->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(选定值)有关。当我删除它时它有效。

【问题讨论】:

  • 我认为你错过了这个'Category' =&gt; $this-&gt;input-&gt;post('categories') 的字段名称categorieen
  • 看看我的最后一行。我刚刚编辑了那个。它说:它与我的 $selectie (选定值)有关。当我删除它时它有效。
  • 这就是这部分:key($selectie)

标签: php mysql forms codeigniter sql-update


【解决方案1】:

原来是下面这行代码出了问题:

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); 

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

【讨论】:

    【解决方案2】:

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

    试试这个

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

    参考见this 希望对你有帮助

    【讨论】:

    • 嗯,我刚刚发现这是问题 ` if($this->db->affected_rows() >= 1) { ` 没有它工作正常。
    • 你知道我是怎么解决的吗?因为我认为这是必需的。
    猜你喜欢
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2016-11-27
    相关资源
    最近更新 更多