Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda