3

I want to insert data in database table using loop. For this I first need to get data of Account_id from user profile table. When user is registered with my website, his/her name table is create automatically, I am storing this table name in Account_id. that's why I must need this field.

First I get all registered users Account_id value. And then I am searching this table using for loop in model.

After I find this table, the row should be inserted, which is created dynamically like

$data = array(
    'Paper_Name' => $paper_name,
    'Paper_Type' => $paper_type,
    'paid_type' => $paid_type
);

Here is my model code.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class create_main_table_model extends CI_Model {

    function login($data){

        $query = $this->db->insert('main_table', $data);    

        $update_userdata = $this->db->select('Account_id')->from('userprofile')->get();

                for($i =0; $i < $update_userdata->num_rows(); $i++){

                        $query = $this->db->insert($update_userdata[$i], $data);    

                }

        return ($this->db->affected_rows() > 0) ? true : false;
    }
}
?>

I want to Insert data to tables which is available in Account_id. If this is possible with other way then please tell me.

5
  • And the issue is??? Commented May 3, 2017 at 4:42
  • $this->db->insert ( $table, $data ); this is insert query in codeigniter Commented May 3, 2017 at 4:52
  • 1
    your almost right change this line $query = $this->db->insert($update_userdata[$i]->Account_id, $data); Commented May 3, 2017 at 4:55
  • you need to update the already existing row or you need new row ? Commented May 3, 2017 at 4:57
  • did you tried my answer Commented May 3, 2017 at 5:07

3 Answers 3

3

This is perfect solution.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class create_main_table_model extends CI_Model {

    function login($data){

        $pass_data = $data;
        $query = $this->db->insert('main_table', $data);    

        $update_userdata = $this->db->select('Account_id')->from('userprofile')->get();

                for($i =0; $i < $update_userdata->num_rows(); $i++){
                        $data1 = $update_userdata->result();
                        $query = $this->db->insert($data1[$i]->Account_id, $pass_data); 

                }

        return ($this->db->affected_rows() > 0) ? true : false;
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class create_main_table_model extends CI_Model {

    function login($data){

        $query = $this->db->insert('main_table', $data);    

        $update_userdata = $this->db->select('Account_id')->from('userprofile')->get();

                for($i =0; $i < $update_userdata->num_rows(); $i++){

                $data['Account_id']=$update_userdata[$i]->Account_id;

                $query = $this->db->insert('table_name', $data);    

                }

        return ($this->db->affected_rows() > 0) ? true : false;
    }
}
?>

1 Comment

I want to tell you again table name is Account_id. so use it as table name
1

Your almost right change this line to get the table name .

$update_userdata = $this->db->$update_userdata->result();
$query = $this->db->insert($update_userdata[$i]->Account_id, $data);

1 Comment

I must get result before using this . like$this->db->$update_userdata->result();

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.