【问题标题】:inserting data through form from one table to another通过表单将数据从一个表插入到另一个表
【发布时间】:2023-03-17 03:08:01
【问题描述】:

我正在使用 php 通过 html 表单将值从一个表插入到另一个表,但问题是另一个表有一些额外的字段说我的表是

table1(姓名、电子邮件、密码、地址、电话号码)

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

如何在我输入其他值的同时通过表单插入t_mobnot_gender

【问题讨论】:

  • 只是显示一些代码,你是如何实现这一目标的
  • $query1="INSERT INTO table1(name, email, password, address, phno) VALUES('$name', '$emailid', '$pwd', '$addr', '$ phno')"; $query2="INSERT INTO table2(t_name, t_email, t_password) SELECT name, email, password FROM table1 WHERE email='$emailid')"; $query3=" INSERT INTO table2(t_mobno, t_gender) VALUES('$t_mobno','$t_gender') where t_email='$emailid'";这就是我尝试的方式,但 where 子句在插入中不起作用。

标签: php mysql html mysqli


【解决方案1】:

请参考: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;
}

【讨论】:

  • 先生,我必须通过表格输入的其他字段呢?
  • @abizz 查看更新的代码,我还没有测试过,但它可能对你有帮助:)
  • @abizz 也许我误会了你是不是想从一个表单中将数据插入两个表?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-08
  • 2014-10-15
  • 1970-01-01
  • 2019-07-18
相关资源
最近更新 更多