2012-11-19 127 views
1

我使用codeigniter將數據插入數據庫。但是,在出現此錯誤將數據保存到數據庫時出現問題

public function saveUserProfile() { 
    $userId = $this->readUserId(); 

    $profile = array(
      'loginid'  => $userId, 
      'name'   => $this->input->post('name'), 
      'email'   => $this->input->post('email'), 
      'gender'  => $this->input->post('gender'), 
      'birthday'  => $this->input->post('birthday'), 
      'profilePhoto' => '', 
      'date'   => date("Y/m/d") 
     ); 

    $query = $this->db->insert('profile', $profile); 

    if ($query) { 
     return true; 
    } else { 
     return false; 
    } 
} 

UPDATE

這裏是它不斷給我下面的錯誤

Unknown column 'Array' in 'field list'</p><p>INSERT INTO `profile` (`loginid`, `name`, `email`, `gender`, `birthday`, `profilePhoto`, `date`) VALUES (Array, 'Zafar Khan', '[email protected]', 'male', '20.01.1987', '', '2012/11/19') 

Filename: /Applications/XAMPP/xamppfiles/htdocs/membership_system/models/model_register.php</p><p>Line Number: 48 

這裏是代碼readUserId功能

private function readUserId() { 
    $this->db->select('id'); 
    $this->db->from('login'); 
    $this->db->where('email', $this->input->post('email')); 

    return $this->db->get()->result(); 
} 

更新2

這是我的輸出的var_dump

array(1) { [0]=> object(stdClass)#18 (1) { ["id"]=> string(2) "14" } } 
+0

更改'date'數組名稱,然後檢查。 –

回答

2

似乎$this->readUserId()返回一個數組。 你可以試試reset($this->readUserId());? 在初始化$ profile之前加上它。

或者只是

echo var_dump($this->readUserId()); 

die(); 

..所以,我們可以看到什麼是你的變量中。

==>編輯:

$配置文件必須成爲像

$profile = array(
      'loginid'  => $userId['id'], 
      'name'   => $this->input->post('name'), 
      'email'   => $this->input->post('email'), 
      'gender'  => $this->input->post('gender'), 
      'birthday'  => $this->input->post('birthday'), 
      'profilePhoto' => '', 
      'date'   => date("Y/m/d") 
     ); 

==>編輯2:

好吧,那麼你可以添加

$userId = $userId[0]->id; 

之前$ profile變量的初始化。

+0

是的,它返回數組。我怎樣才能解決這個問題?我只需要這個數組的id。 – 2619

+0

我在我的文章中添加了readUserId函數。請檢查一下。 – 2619

+0

好的謝謝,但我不知道這種方法最終返回的是什麼。你能告訴我var_dump嗎? – Maxime

4

$userId的值導致一個不帶引號Array被輸入到正被處理爲一列參考值。

0

你的「userid」字段是一個數組。您的帖子中存在另一個「用戶標識」。 在您的視圖文件中查找重複的名稱。

0

問題似乎出現在$ this-> readUserId()中,它返回一個數組來代替標量。請檢查一下。