2
votes

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.

1

1 Answers

0
votes

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