Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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;
    }
}
?>
about 4 years ago · Santiago Trujillo Report

0

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);
about 4 years ago · Santiago Trujillo Report

0

<?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;
    }
}
?>
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!