我有2个表es_user和es_user_detail,user.id = user_detail.user_id的关系。
我在application / config / database.php文件中定义了前缀“es_”:
$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
答案 0 :(得分:0)
在没有查看CodeIgniter的内容的情况下,我认为Active Record解析器因为提交了一个包含两个表名和表别名的字符串而感到困惑。
您有几个选择:
(1)如果您想使用Active Record,只需使用两个$this->db->update()
来一个user
,一个来user_detail
。您需要对WHERE
条款进行细微更改。
(2)将SQL语句手动编码为字符串并将其传递给$this->db->query()
,这可能需要做更多的工作。
在我的应用程序中,我倾向于使用方法(1)。
如果数据完整性至关重要,请使用InnoDB
作为存储引擎,并使用CodeIgniter的交易功能,这很容易使用。