0

Is there a way that I can update a form input using something like insert_id? I have a form for adding factories to a table and I use the insert_id to store the factories id in another table too after submit.

this all works great, but is there such a way for $this->db->update?

My model for updating:

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

My controller function for updating:

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

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

for adding factories i use this with insert_id:

controller:

function addbedrijven()
{
    $this->members_model->addbedrijf();
    redirect('members/index');
}

model:

function addbedrijf() 
{ 
    $data1 = 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'), 
        'logo' => $this->input->post('logo') 
    ); 
    $this->db->insert('bedrijven',$data1); 

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

    $this->insert_bedrijfcat($to_bedrijfcategorieen); 
    }else{ 
    return FALSE;
    } 
} 

function insert_bedrijfcat($data1) 
{ 
    $this->db->insert('bedrijfcategorieen', $data1); 

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

Hope it's clear enough.

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
  • Just as a sidenode, you are doing direct DB interaction in your controller and user interaction in the model. Your MVC principle is getting a bit clouded. updatebedrijven should ask the domain level (of which the models are part) for the data requested, and the controller should tell the model what input the user has provided. – nvanesch May 01 '13 at 11:56
  • thanks. I got that from an answer here on SO earlier and i never edited it. so i will do. but i need an answer on my question ;( – Kees Sonnema May 01 '13 at 12:00
  • The question is a bit unclear to me. some questions. the company<->category relation, is this many-to-many? what is the type(datatype) of field idcategorieen? also another tip for future code, write code in english, that way more people than us dutchies can understand function names and properties :D – nvanesch May 01 '13 at 12:18
  • sorry if it's a bit unclear. i have a 3 table join. my table for joining categories and companies is called factorycategories (translated). i have idfactories, idcategories and idfactorycategories in that table. so multiple categories could belong to multiple factories. and vise versa – Kees Sonnema May 01 '13 at 12:22
  • ok and one more question: is $this->input->post('categorieen') an array? – nvanesch May 01 '13 at 12:33
  • it's a dropdown on my edit form. i populate the dropdown using the categoryname from the categories table. – Kees Sonnema May 01 '13 at 12:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29242/discussion-between-nvanesch-and-kees-sonnema) – nvanesch May 01 '13 at 12:36

0 Answers0