Insert data through a form from one table to another

I am inserting values ​​from one table to another through an html form using php, but the problem is that the other table has some additional fields that say my tables

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

and

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

how to insert forms t_mobnoand t_genderat the same time as other values.

+4
source share
1 answer

Note: INSERT IN SELECT

This is how I did this can 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 defined array_from_post in my base model, you can use it as follows:

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

All Articles