2013-03-18 107 views
1

我有2個表es_user和es_user_detail,user.id = user_detail.user_id的關係。在codeigniter中一次更新兩個表

我已經定義了前綴 「es_」 中的application/config/database.php中的文件:

$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` = '[email protected]', `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` = '', `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

表'es_ems.user_detail'不存在。 – Red 2013-03-18 08:34:02

+0

表是es_ems.es_user_detail。我知道。我的問題在於,前綴「es_」沒有顯示出來。 $ this-> db-> update('user as us,user_detail as ud'); 在上面的代碼中,用戶轉換爲es_user,但user_detail未轉換爲es_user_detail。 – yajay 2013-03-18 09:30:12

+0

什麼是_ems? – Red 2013-03-18 09:43:35

回答

0

沒有尋找到笨的膽量,我覺得是因爲你已經提交了一個字符串以兩個表名和表別名的活動記錄分析器是越來越糊塗了。

你有兩個選擇:

(1)如果你想使用活動記錄,只需用兩個$this->db->update()調用一個user,一個用於user_detail。您需要對WHERE子句進行細微更改。

(2)將SQL語句手動編碼爲一個字符串並將其傳遞到$this->db->query(),這可能需要更多工作。

在我的應用程序中,我傾向於使用方法(1)。

如果數據完整性至關重要,請使用InnoDB作爲存儲引擎並使用CodeIgniter的Transaction功能,該功能很容易使用。