使用 codeigniter 和 innodb 引擎一次插入两个表 mysql
insert into two tables at once using codeigniter & innodb engine mysql
你好,我有两个 table,一个是 users,另一个是 parent
mysql 查询父项是
CREATE TABLE `parent` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL DEFAULT '0',
`name` longtext NOT NULL,
`dob` int(10) unsigned NOT NULL DEFAULT '0',
`sex` longtext NOT NULL,
`address` longtext NOT NULL,
`contact` longtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
mysql 查询用户是
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` longtext NOT NULL,
`username` longtext NOT NULL,
`password` longtext NOT NULL,
`type` longtext NOT NULL,
`status` int(10) unsigned NOT NULL DEFAULT '0',
`online` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
我有一个包含上述所有详细信息的表格。现在我想将它们插入各自的 table。我也在使用 CSRF 保护
我试过的
我的控制器代码
public function create()
{
$udata=array(
'email'=>$this->input->post('email'),
'password'=>md5($this->input->post('password')),
'type'=>'parent',
'online'=>'0',
'status'=>'1'
);
$pdata=array(
'name'=>$this->input->post('name'),
'dob'=>strtotime($this->input->post('dob')),
'gender'=>$this->input->post('gender'),
'address'=>$this->input->post('address'),
'contact'=>$this->input->post('contact')
);
$pid= $this->user_model->insert($udata,$pdata);
}
我的模型数据
function insert($userdata,$typedata)
{
$this->db->insert('users', $userdata);
$typedata['user_id'] = $this->db->insert_id();
$this->db->insert('parent', $typedata);
return $this->db->insert_id();
}
以上代码仅向第一个 table 即用户插入值,而其他 table 即父级为空。
请注意:parent table 的 user_id 字段是 primary key (auto increment) 用户table
问题已通过使用错误记录解决
谢谢@joerg
最好在模型中使用单独的函数插入 class,以避免错误
function insertUser($userdata,$typedata)
{
$this->db->insert('users', $userdata);
return $this->db->insert_id();
}
function insertParent($typedata)
{
$this->db->insert('parent', $typedata);
return $this->db->insert_id();
}
你好,我有两个 table,一个是 users,另一个是 parent
mysql 查询父项是
CREATE TABLE `parent` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL DEFAULT '0',
`name` longtext NOT NULL,
`dob` int(10) unsigned NOT NULL DEFAULT '0',
`sex` longtext NOT NULL,
`address` longtext NOT NULL,
`contact` longtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
mysql 查询用户是
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` longtext NOT NULL,
`username` longtext NOT NULL,
`password` longtext NOT NULL,
`type` longtext NOT NULL,
`status` int(10) unsigned NOT NULL DEFAULT '0',
`online` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
我有一个包含上述所有详细信息的表格。现在我想将它们插入各自的 table。我也在使用 CSRF 保护
我试过的
我的控制器代码
public function create()
{
$udata=array(
'email'=>$this->input->post('email'),
'password'=>md5($this->input->post('password')),
'type'=>'parent',
'online'=>'0',
'status'=>'1'
);
$pdata=array(
'name'=>$this->input->post('name'),
'dob'=>strtotime($this->input->post('dob')),
'gender'=>$this->input->post('gender'),
'address'=>$this->input->post('address'),
'contact'=>$this->input->post('contact')
);
$pid= $this->user_model->insert($udata,$pdata);
}
我的模型数据
function insert($userdata,$typedata)
{
$this->db->insert('users', $userdata);
$typedata['user_id'] = $this->db->insert_id();
$this->db->insert('parent', $typedata);
return $this->db->insert_id();
}
以上代码仅向第一个 table 即用户插入值,而其他 table 即父级为空。
请注意:parent table 的 user_id 字段是 primary key (auto increment) 用户table
问题已通过使用错误记录解决 谢谢@joerg
最好在模型中使用单独的函数插入 class,以避免错误
function insertUser($userdata,$typedata)
{
$this->db->insert('users', $userdata);
return $this->db->insert_id();
}
function insertParent($typedata)
{
$this->db->insert('parent', $typedata);
return $this->db->insert_id();
}