2016-06-24 51 views
0

我試圖使用提交的表單插入數據。數據在數據庫中正確輸入,但我也在爲移動設備製作API。如果收到is_api參數,我在做jsone_encode。以下是代碼:Codeigniter:您必須使用「set」方法更新條目

控制器:

public function send_request(){ 
      if($this->input->post('submit')){   
       $user_data['data'] = array( 
        'user_id'    => 1, 
        'sender_name'   =>  $this->input->post('sender_name'), 
        'sender_location'  =>  $this->input->post('sender_location'), 
        'sender_mobile'   =>  $this->input->post('sender_mobile'), 
        'receiver_name'   =>  $this->input->post('reciever_name'), 
        'receiver_location'  =>  $this->input->post('reciver_location'), 
        'receiver_mobile'  =>  $this->input->post('reciver_location'), 
        'request_type'   =>  $this->input->post('request_type'), 
        'is_urget'    =>  0, 
       ); 

      } 
      $result = $this->user_model->send_request($user_data); 
      if($result){ 
       $this->result_set['message'] = 'Data entered '; 
       $this->result_set['status']= 1; 
      } 
       if($this->input->post('is_api') == 1){ 
        echo json_encode($this->result_set); 
        die(); 
       } 
     } 

型號:

public function send_request($data){ 
     $this->db->insert('parcel_requests',$data['data']); 
     return true; 
    } 

當我檢查hurl.it的API,我得到這個錯誤和響應:

發生數據庫錯誤

您必須使用「set」方法更新條目。

文件名:/home/foldername/public_html/parcel/models/user_model.php

行號:21

這是行號21:現在進入

$this->db->insert('parcel_requests',$data['data']); 

數據正確,但未轉換爲json。我需要你的幫助來解決這個問題,謝謝!

+1

您是否嘗試過只是將$ data數組傳遞給插入函數而不是$ data ['data']? – Franco

+0

用'$ sql = $ this-> db-> set($ data ['data']) - > get_compiled_insert('parcel_requests')檢查查詢字符串。 echo $ sql;' – BitAccesser

回答

0

檢查:

public function send_request(){ 
    if($this->input->post('submit')){ 
     $user_data['data'] = array(
      'user_id'    => 1, 
      'sender_name'   =>  $this->input->post('sender_name'), 
      'sender_location'  =>  $this->input->post('sender_location'), 
      'sender_mobile'   =>  $this->input->post('sender_mobile'), 
      'receiver_name'   =>  $this->input->post('reciever_name'), 
      'receiver_location'  =>  $this->input->post('reciver_location'), 
      'receiver_mobile'  =>  $this->input->post('reciver_location'), 
      'request_type'   =>  $this->input->post('request_type'), 
      'is_urget'    =>  0, 
     ); 
     // inside your if ;), you should insert to database only if you submit data... 

     $result = $this->user_model->send_request($user_data); 
     if($result){ 
      $this->result_set['message'] = 'Data entered '; 
      $this->result_set['status']= 1; 
     } 
     if($this->input->post('is_api') == 1){ 
      echo json_encode($this->result_set); 
      die(); 
     } 
    } else { 
     print 'nthg sumbited'; 
    } 
} 

public function send_request($data){ 
    $this->db->insert('parcel_requests',$data['data']); 
    return $this->db->affected_rows() == 1; 
    // Will return true after insert will be ended with success 
} 

在你的版本有可能發送空數組:)而這就是爲什麼這個異常顯示:)

0

改變你的$user_data['data'] = array$data['data'] = array

相關問題