Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert data to table using loop.?

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.

like image 554
moje_mast_ram Avatar asked May 21 '26 18:05

moje_mast_ram


2 Answers

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;
    }
}
?>
like image 70
moje_mast_ram Avatar answered May 24 '26 07:05

moje_mast_ram


<?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;
    }
}
?>
like image 39
Hothi Jimit Avatar answered May 24 '26 09:05

Hothi Jimit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!