简体   繁体   English

提交表单时如何自动将FactoryID和CategoryID插入表格中-Codeigniter

[英]How to automatically insert factoryid and categoryid into a table when submitting form - codeigniter

I am using 3 tables for joining. 我正在使用3个表进行联接。 I have a form where i can add a factory. 我有一个可以添加工厂的表格。 I also can choose a category for that specific factory. 我还可以为该特定工厂选择类别。 i use the following tables: 我使用下表:

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

I want to insert the last inserted idcategories and idfactories in my factorycategories table. 我想在我的factorycategories表中插入最后插入的idcategories和idfactories。 How can i do this? 我怎样才能做到这一点?

My model for inserting the form values: 我的插入表单值的模型:

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

My model function for showing categories and factories on the same page: 我的模型函数用于在同一页面上显示类别和工厂:

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

Hope i provided enough code for you guys. 希望我为你们提供了足够的代码。

Edit: 编辑:

the code that i have so far using the example below: 到目前为止,我使用以下示例提供的代码:

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

You are inserting data to the bedrijven table 您正在将数据插入到bedrijven表中

and you want to get the last inserted id of 并且您想要获取最后插入的ID

categories and factories table then insert it to factorycategories table also? categoriesfactories表然后将其插入到factorycategories表还?

THe question is when inserting data to bedrijven table do you insert data to the 问题是,当将数据插入到bedrijven表中时,是否将数据插入到

categories and factories table ? categoriesfactories表? Because if you do you can use $this->db->insert_id to get the last input id of your categories and factories table, if not you need to query a different sql to get both last input id. 因为如果这样做,您可以使用$this->db->insert_id来获取categoriesfactories表的最后一个输入ID,否则,您需要查询其他sql来获取最后一个输入ID。

EDIT 编辑

here so i modified a some code example 在这里,所以我修改了一些代码示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM