Tuesday, 27 October 2015

MEMASUKKAN DATA KE DALAM 2 TABLE MENGGUNAKAN CODEIGNITER

Memasukkan Data Ke Dalam 2 Table menggunakan CodeIgniter
1-Table User
  • user_id(int)
  • user_email(varchar(64))
  • user_name(varchar(64))
  • user_pass(varchar(64))
2-Table Profiles
  • prof_id(int)
  • user_id(int)
  • first_name(varchar(64))
  • last_name(varchar(64))
Controller : Umpukkan 2 Variable dalam 2 Jenis Array berbeza ($data1 & $data2)
function add_account() {
    $this->load->model('m_signup');
    // get form variable
    $first_name = $this->input->post('first_name');
    $last_name = $this->input->post('last_name');
    $user_email = $this->input->post('user_email');
    $user_name = $this->input->post('user_name');
    $user_pass = $this->input->post('user_pass');


    $data1 = array($user_name, $user_email, $user_pass);
    $data2 = array($first_name, $last_name);

    $this->m_signup->add_account($data1, $data2);

    redirect('login');
}
Models  
function add_account($data1, $data2) {
    $this->db->trans_start();

    $sql1 = "INSERT INTO users(user_name, user_email, user_pass) 
            VALUES (?, ?, ?)";

    $this->db->query($sql1, $data1); 
    $id_user = $this->db->insert_id(); 

    $sql2 = "INSERT INTO profiles(user_id, first_name, last_name) 
            VALUES ($id_user, ?, ?)"; 
    $this->db->query($sql2, $data2);

   $this->db->trans_complete(); 

  return $this->db->insert_id(); 

}
Abd Rahman Bin Sirat

Rujukan : http://stackoverflow.com/questions/21076413/insert-array-data-into-two-table-using-codeigniter
   

0 comments:

Post a Comment