的主键(自动递增)
你好,我有两个表一个是用户 &
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;
我有一个包含上述所有细节的表单。现在我想把它们插入到各自的表中。我也使用CSRF保护
What I have try
MY Controller Code
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);
}
My MODEL Data
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();
}
上面的代码只向第一个表即users插入值,其他表即parent为空。
请注意:父表的user_id字段是users表
使用错误记录解决问题由于@joerg
最好在模型类中使用单独的函数进行插入,以避免错误
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();
}