2

I have a registration form and will inserting data inputed into two different table using registration function. This is the table structure:

user

  • user_id(int)
  • user_email(varchar(64))
  • user_name(varchar(64))
  • user_pass(varchar(64))

profiles

  • prof_id(int)
  • user_id(int)
  • first_name(varchar(64))
  • last_name(varchar(64))
  • ...(other field)

I put the variables into two different array ($data1 & $data2) here is my controller:

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

and the model was like this:

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

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

    $this->db->trans_complete(); 

}

When I submit the registration, its work in table users, but the table profiles still going empty. Please help me correct my add_account function above.

3 Answers 3

1

Exchange the lines: Line $this->db->trans_complete(); is not reachable.

You are returning id before committing the transaction.

 $this->db->trans_complete(); 

 return $this->db->insert_id(); 
Sign up to request clarification or add additional context in comments.

4 Comments

thanks kumar, could you give me a guidance how to join both table using user_id as foreign key
Are asking for getting record from two table? Or do you want to create foreign key?
I wanna create foreign key in users table so I can do CRUD operations in my models.
0

in controller

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

    

    $data1_id = $this->m_signup->add_account($data1);


    $data2 = array($data1_id,$first_name, $last_name,);

    $data_id = $this->m_signup->add_account($data2,$data1_id);

    redirect('login');
}

Comments

0

in model

function add_account($data1 = '', $data2 = '' , $id = '') {
    $this->db->trans_start();

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

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

    }else{
    $sql2 = "INSERT INTO profiles(user_id, first_name, last_name) 
            VALUES (?, ?, ?)"; 
    $this->db->query($sql2, $data2);
    }
    return $this->db->insert_id(); 

    $this->db->trans_complete(); 

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.