简体   繁体   English

通过表单将数据从一个表插入另一个表

[英]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 我正在使用php通过html表单将值从一个表插入另一个表,但问题是另一个表有一些额外的字段说我的表是

table1(name, email, password, address, phno) table1(姓名,电子邮件,密码,地址,phno)

and

table2(t_name, t_email, t_password, t_mobno, t_gender) 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. 如何在输入其他值的同时插入t_mobnot_gender

Refer this: INSERT INTO SELECT 请参阅: 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: 在这里我已经在我的基础模型中定义了array_from_post,您可以像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM