我正在使用Codeigniter,以下代码正在工作并插入到两个InnoDB表中,但我无法将Insert_ID插入到第二个表中 到目前为止,这是我的代码的一部分。
控制器:
//Create Customer Data Array
$data1 = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'zipcode' => $this->input->post('zipcode'),
'refferedby' => $this->input->post('refferedby')
);
//$insert_id =$this->Ticket_model->insert_data($data1);
$data2 = array(
//'user_id' => $this->Ticket_model->insert_id(),
'section' => $this->input->post('section'),
'make' => $this->input->post('make'),
'problem' => $this->input->post('problem'),
'notes' => $this->input->post('notes'),
'assets' => $this->input->post('assets'),
'password' => $this->input->post('password'),
'imei' => $this->input->post('imei'),
'cost' => $this->input->post('cost'),
'assignedto' => $this->input->post('assignedto'),
'created' => $this->input->post('created'),
'createdby' => $this->input->post('createdby'),
'promisedate' => $this->input->post('promisedate'),
'status' => $this->input->post('status')
);
$data['insert'] = $this->Ticket_model->insert_data($data1,$data2);
//View
$data['main_content'] = 'admin/orders/addtick';
$this->load->view('admin/layouts/main',$data);
型号:
public function insert_data($data1,$data2){
$this->db->trans_start();
//$this->db->query("INSERT INTO customers($data1) VALUES ()");
$this->db->insert('customers', $data1);
$user_id=$this->db->insert_id();
//$this->db->insert('tickets', $data2);
$this->db->insert('tickets', $data2);
$this->db->trans_complete();
return $this->db->insert_id();
}
数据库:客户表
CREATE TABLE IF NOT EXISTS `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`phone` int(15) NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(50) NOT NULL,
`zipcode` int(5) NOT NULL,
`refferedby` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;
门票表:
CREATE TABLE IF NOT EXISTS `tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`section` varchar(25) NOT NULL,
`user_id` int(11) NOT NULL,
`make` varchar(255) NOT NULL,
`problem` text NOT NULL,
`notes` text NOT NULL,
`assets` varchar(100) NOT NULL,
`password` varchar(25) NOT NULL,
`imei` varchar(25) NOT NULL,
`cost` double NOT NULL,
`assignedto` varchar(25) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`createdby` varchar(50) NOT NULL,
`promisedate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`status` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
感谢您的帮助。
答案 0 :(得分:1)
我会在模型中使用两种方法来简化疑难解答查询
public function insert_data1($data1){
$this->db->trans_start();
//$this->db->query("INSERT INTO customers($data1) VALUES ()");
$this->db->insert('customers', $data1);
$user_id=$this->db->insert_id();
$this->db->trans_complete();
return $user_id;
}
和
public function insert_data2($data2){
$this->db->trans_start();
//$this->db->insert('tickets', $data2);
$this->db->insert('tickets', $data2);
$ticket_id=$this->db->insert_id();
$this->db->trans_complete();
return $ticket_id;
}
这是控制器
public function foo()
{
//Create Customer Data Array
$data1 = array
(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'zipcode' => $this->input->post('zipcode'),
'refferedby' => $this->input->post('refferedby')
);
$this->load->model("tickets_model");
$last_user_id = $this->tickets_model->insert_data1($data1);
$data2 = array
(
//$last_user_id is the value of the foreign key to link the tables
'user_id' => $last_user_id,
'section' => $this->input->post('section'),
'make' => $this->input->post('make'),
'problem' => $this->input->post('problem'),
'notes' => $this->input->post('notes'),
'assets' => $this->input->post('assets'),
'password' => $this->input->post('password'),
'imei' => $this->input->post('imei'),
'cost' => $this->input->post('cost'),
'assignedto' => $this->input->post('assignedto'),
'created' => $this->input->post('created'),
'createdby' => $this->input->post('createdby'),
'promisedate' => $this->input->post('promisedate'),
'status' => $this->input->post('status')
);
$last_ticket_id = $this->tickets_model->insert_data2($data2);
$data['insert'] = $last_ticket_id;
$data['main_content'] = 'admin/orders/addtick';
//View
$this->load->view('admin/layouts/main',$data);
}
这是runnable.com下的完整模型,控制器和视图代码
希望有所帮助