Abizz
Abizz

Reputation: 216

inserting data through form from one table to another

I'm inserting values from one table to another through html form using php, but the problem is the other table is having some extra fields say my tables are

table1(name, email, password, address, phno)

and

table2(t_name, t_email, t_password, t_mobno, t_gender)

how to insert the t_mobno and t_gender through form same time as i'm entering the other values.

Upvotes: 4

Views: 256

Answers (1)

Rajan
Rajan

Reputation: 2425

Refer this:INSERT INTO SELECT

This Is How I Did it, might help :

$query = $this->db->get('Table1')->result(); // get first table
foreach($query as $result) 
{ 
//Your Post Values from form data in array : POST_DATA
$post_data = array_from_post('filed1,filed2....');

$data = array_merge($result,$post_data);

$this->db->insert('Table2', $data); // insert each row to another table
}

Here I have defined array_from_post in my base model you may use it like this:

public function array_from_post($fields){
    $data = array();
    foreach ($fields as $field) {
        $data[$field] = $this->input->post($field);
    }
    return $data;
}

Upvotes: 1

Related Questions