如何在codeigniter中将某些表的id插入到另一个表中

时间:2015-01-07 08:48:50

标签: php mysql codeigniter

我在插入此数组的第一个表中插入2个表

   array(2) {
  [0]=>
  array(5) {
    ["language_id"]=>
    string(1) "3"
    ["property_id"]=>
    int(82)
    ["option_id"]=>
    string(2) "10"
    ["value"]=>
    string(0) ""
    ["value_num"]=>
    NULL
  }

并在第二个插入此数组

array(2) {
["property_type_id"]=>
string(2) "21"
["alt_txt"]=>
string(7) "4666.45"}

这是我在插入控制器中的函数

    public function save_dynamic($data, $id)
{
    // Delete all
    $this->db->where('property_id', $id);
    $this->db->where('value !=', 'SKIP_ON_EMPTY');
    $this->db->delete('property_value'); 

    // Insert all
    $insert_batch = array();
    foreach($data as $key=>$value)
    {
        if(substr($key, 0, 6) == 'option')
        {
            $pos = strpos($key, '_');
            $option_id = substr($key, 6, $pos-6);
            $language_id = substr($key, $pos+1);

            $val_numeric = NULL;
            if( is_numeric($value) )
            {
                $val_numeric = intval($value);
            }

            $insert_arr = array('language_id' => $language_id,
                                'property_id' => $id,
                                'option_id' => $option_id,
                                'value' => $value,
                                'value_num' => $val_numeric);

            if($value != 'SKIP_ON_EMPTY')
                $insert_batch[] = $insert_arr;


        }
    }

                        $data=$this->input->post('data');
    if(count($insert_batch) > 0)
        $this->db->insert_batch('property_value', $insert_batch); 
    if($this->db->affected_rows() > 0)
        $this->db->insert_batch('property_type_details', $data);

以下是我的表格的样子

Field Type
id int(11) NOT NULL
language_id int(11) NOT NULL
property_id int(11) NOT NULL
option_id int(11) NOT NULL
value text NULL
value_num int(11) NULL

Field Type
id int(10) unsigned NOT NULL
property_id int(10) NULL
property_type_id int(10) NULL
alt_txt double(10,2) NULL

所以我想要做的是在第二个表中插入property_id和第二个数组

2 个答案:

答案 0 :(得分:0)

使用codeigniter返回$this->db->insert_id()或$ get_id = $this->db->insert_id

以下是我从另一张桌子获取身份证的方式:

public function addBanner() {
    $data = array('name' => $this->input->post('name'),'status' =>   $this->input->post('status'));
    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert('banner');

    $banner_id = $this->db->insert_id();

    $data = array(
        'banner_id' => (int)$banner_id,
        'image' => $data_image['file_name'],
        'link' => $link
    );


    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert('banner_image');
    }
}

return $banner_id;
}

答案 1 :(得分:0)

public function save_dynamic($data, $id)
{
    // Delete all
    $this->db->where('property_id', $id);
    $this->db->where('value !=', 'SKIP_ON_EMPTY');
    $this->db->delete('property_value'); 

    // Insert all
    $insert_batch = array();
    foreach($data as $key=>$value)
    {
        if(substr($key, 0, 6) == 'option')
        {
            $pos = strpos($key, '_');
            $option_id = substr($key, 6, $pos-6);
            $language_id = substr($key, $pos+1);

            $val_numeric = NULL;
            if( is_numeric($value) )
            {
                $val_numeric = intval($value);
            }

            $insert_arr = array('language_id' => $language_id,
                                'property_id' => $id,
                                'option_id' => $option_id,
                                'value' => $value,
                                'value_num' => $val_numeric);

            if($value != 'SKIP_ON_EMPTY')
                $insert_batch[] = $insert_arr;


        }
       $data=$this->input->post('data');
       $this->db->insert('property_value', $insert_batch);
       $data['property_id']=$this->db->insert_id();
       $this->db->insert('property_type_details', $data);
    }
 }