0

我正在使用 3 个表来加入。我有一个可以添加工厂的表格。我还可以为该特定工厂选择一个类别。我使用下表:

factorycategories
-----------------
idfactorycategories
idfactories
idcategories
(in this table i use a join to show the categories and factories on one page)

factories
---------
idfactories
factoryname
address
country
telephone
...
...

categories
----------
idcategories
category

我想在我的 factorycategories 表中插入最后插入的 idcategories 和 idfactories。我怎样才能做到这一点?

我插入表单值的模型:

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

我在同一页面上显示类别和工厂的模型功能:

function get_result($idbedrijven)
{
    $this->db->where('bedrijven.idbedrijven', $idbedrijven);
    $this->db->join('bedrijven', 'bedrijfcategorieen.idbedrijven = bedrijven.idbedrijven');
    $this->db->join('categorieen', 'bedrijfcategorieen.idcategorieen = categorieen.idcategorieen');
    $this->db->group_by('Categorie', 'idbedrijven', 'idcategorieen');
    $result = $this->db->get('bedrijfcategorieen', 1);
    return $result->result(); 
}

希望我为你们提供了足够的代码。

编辑:

到目前为止,我使用以下示例的代码:

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)
    {
    $this->insert_bedrijfcat($this->db->insert_id);
    }else{
    return FALSE
    }
}

function insert_bedrijfcat($id)
{
    $this->db->insert('bedrijfcategorieen',array('idbedrijven'=>$id));

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

1 回答 1

0

您正在向bedrijven表中插入数据

并且您想获取最后插入的 id

categories然后表格也factories将其插入factorycategories表格?

问题是当向bedrijven表中插入数据时,您是否将数据插入到表中?

categoriesfactories表?因为如果你这样做,你可以$this->db->insert_id用来获取你categoriesfactories表的最后一个输入 id,如果不是,你需要查询不同的 sql 来获取最后一个输入 id。

编辑

在这里,我修改了一些代码示例

class Some_model Extends CI_Model
{
   public function insert_factory($data)
   {
      $this->db->insert('factory_table',$data);

      //if has affected rows
      if($this->db->affected_rows() >= 1)
      {
        // the insert was successfull send the last insert id to another method
        $this->insert_to_factorycateg($this->db->insert_id);
      }else{
         return FALSE
     }
   }

   //method to insert to factorycateg
   public function insert_to_factorycateg($id)
   {
       //insert 
      $this->db->insert('factorycategories',array('factoryid'=>$id));

      //if affected rows return true else false
      return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
   }
}
于 2013-04-26T08:11:16.817 回答