Php 在codeigniter中一次更新两个表

Php 在codeigniter中一次更新两个表,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有两个表es_user和es_user_detail,user.id=user_detail.user_id的关系 我已经在application/config/database.php文件中定义了前缀es_u: $db['default']['dbprefix'] = 'es_'; 现在,我编写了同时更新两个表的代码 $this->db->set('us.prefix',$this->input->post('prefix',TRUE)); $this->db

我有两个表es_user和es_user_detail,user.id=user_detail.user_id的关系

我已经在application/config/database.php文件中定义了前缀es_u:

$db['default']['dbprefix'] = 'es_';
现在,我编写了同时更新两个表的代码

$this->db->set('us.prefix',$this->input->post('prefix',TRUE));
$this->db->set('us.first_name',$this->input->post('first_name',TRUE));            
$this->db->set('us.last_name',$this->input->post('last_name',TRUE));
$this->db->set('us.gender',$this->input->post('gender',TRUE));

$this->db->set('us.email',$this->input->post('email',TRUE));
$this->db->set('ud.home_number',$this->input->post('home_number',TRUE));
$this->db->set('ud.mobile_number',$this->input->post('mobile_number',TRUE));
$this->db->set('ud.address',$this->input->post('address',TRUE));
$this->db->set('ud.address1',$this->input->post('address1',TRUE));

$this->db->set('ud.state',$this->input->post('state',TRUE));
$this->db->set('ud.zip',$this->input->post('zip',TRUE));
$this->db->set('ud.country',$this->input->post('country',TRUE));
$this->db->set('ud.work_job_title',$this->input->post('work_job_title',TRUE));
$this->db->set('ud.work_company',$this->input->post('work_company',TRUE));
$this->db->set('ud.work_address',$this->input->post('work_address',TRUE));
$this->db->set('ud.work_city',$this->input->post('work_city',TRUE));
$this->db->set('ud.work_state',$this->input->post('work_state',TRUE));
$this->db->set('ud.work_country',$this->input->post('work_country',TRUE));
$this->db->set('ud.work_website',$this->input->post('work_website',TRUE));
$this->db->set('ud.email_update_notify',$this->input->post('email_update_notify',TRUE));



$this->db->set('ud.email_event_notify',$this->input->post('email_event_notify',TRUE));
$this->db->set('us.last_modify_date',$this->general->get_local_time('time'));
$this->db->where("user_id", $this->session->userdata(SESSION.'user_id'));
$this->db->where("us.id = ud.user_id");
$this->db->update('user as us, user_detail as ud');
更新表失败。 我从这里得到的错误是:

Table 'es_ems.user_detail' doesn't exist

UPDATE `es_user` as us, user_detail as ud SET `es_us`.`prefix` = 'mr', `es_us`.`first_name` = 'xyz', `es_us`.`last_name` = 'abc', `es_us`.`gender` = 'm', `es_us`.`email` = 'xyz@gmail.com', `es_ud`.`home_number` = '898939958', `es_ud`.`mobile_number` = '9841844058', `es_ud`.`address` = 'chabahil', `es_ud`.`address1` = 'ason', `es_ud`.`state` = 0, `es_ud`.`zip` = '01234', `es_ud`.`country` = 'country', `es_ud`.`work_job_title` = 'PHP Developer', `es_ud`.`work_company` = 'pqr company', `es_ud`.`work_address` = 'work address', `es_ud`.`work_city` = 'city', `es_ud`.`work_state` = 'state ', `es_ud`.`work_country` = 'country', `es_ud`.`work_website` = 'http://www.pqr.com', `es_ud`.`email_update_notify` = '1', `es_ud`.`email_event_notify` = '1', `es_us`.`last_modify_date` = '2013-03-18 13:46:22' WHERE `user_id` = '6' AND `es_us`.`id` = ud.user_id

Filename: D:\wamp\www\ems\system\database\DB_driver.php

没有深入研究CodeIgniter的本质,我认为Active Record解析器正在变得混乱,因为您提交了一个包含两个表名和表别名的字符串

您有两个选择:

1如果您想使用活动记录,只需使用两个$this->db->update调用,一个用于user,另一个用于user\u detail。您需要对WHERE子句进行细微更改

2将SQL语句手工编码为字符串,并将其传递到$this->db->query中,这可能需要更多的工作

在我的应用程序中,我倾向于使用方法1


如果数据完整性至关重要,请使用InnoDB作为存储引擎,并使用CodeIgniter的事务功能,该功能非常容易使用。

表“es_ems.user_detail”不存在。表是es_ems.es_user_detail。我知道。我的问题是,前缀es_u没有出现$这->数据库->更新“用户为我们,用户详细信息为ud”;在上面的代码中,user被转换为es_user,但user_detail没有转换为es_user_detail。es_ems是包含es_user和es_user_detail表的数据库