phpcodeigniterinsert-id

How to get my ID on codeigniter?


Struckuter So i want when i input blablabla on tproject, automatic id from id project to tproject_pic but when i using blabla, no record on tproject_pic, whts wrong?

Controller

$data = array(
  'id' => '',
  'division' => $division,
  'client'=> $client,
  'siteid' => $siteid,
  'sitename' => $sitename,
  'lat' => $lat,
  'lng' => $lng,
  'location' => $location,
  'address' => $address,
  'description' => $description,
  'account' => $account,
  'datestart' => $datestart,
  'dateclose' => $dateclose,
  'pono' => $pono,
  'podate' => $podate,
  'povalue' => $povalue,
  'status' => $status,
  'status_remark' => $status_remark,
  'datestamp' => date('YmdHis')
);

$data1 = array (
  'pid' => $id,
  'uid' => $uid,
  'title'=> "user"
);

helper_log("add", "menambahkan data");
$this->form_validation->set_rules('siteid', 'Site ID', 'trim|required|alpha_dash|xss_clean|callback_checksite');
//$data['client_dropdown'] = $this->main_model->client_dropdown;
$this->session->set_flashdata('message','<div class="teal">Data telah di update</div>');
$this->main_model->check_site($siteid);
$this->main_model->input_data($data,'tproject');
$this->main_model->input_data($data1,'tproject_pic');
print_r($data1);
exit;
$data['dd_client'] = $this->main_model->dd_client();
$data['dd_lokasi'] = $this->main_model->dd_lokasi();
redirect('page/master');

MODEL

function input_data($data,$table){
  $this->db->insert($table, $data);
  $id = $this->db->insert_id();
  return (isset($id)) ? $id : FALSE;
}

Solution

  • When you insert data in project table successfully, store the returned value in a variable say

    $project_id=$this->main_model->input_data($data,'tproject');
    

    and store that id in the $data1 array as:

    $data1 = array (
    'pid' => $project_id,
    'uid' => $uid,
    'title'=> "user"
    );
    

    and insert it as:

    $this->main_model->input_data($data1,'tproject_pic');