加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:04:07 所属栏目:PHP教程 来源:网络整理
导读:我正在使用php通过html表单将值从一个表插入另一个表,但问题是另一个表有一些额外的字段说我的表是 table1(name,email,password,address,phno) 和 table2(t_name,t_email,t_password,t_mobno,t_gender) 如何在输入其他值的同时插入t_mobno和t_gender. 解决方
我正在使用php通过html表单将值从一个表插入另一个表,但问题是另一个表有一些额外的字段说我的表是

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

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

如何在输入其他值的同时插入t_mobno和t_gender.

解决方法

请参阅: INSERT INTO SELECT

这是我如何做到的,可能会有所帮助:

$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
}

在这里我已经在我的基础模型中定义了array_from_post,您可以像这样使用它:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读