簡體   English   中英

Codeigniter:您必須使用“set”方法來更新條目

[英]Codeigniter: You must use the “set” method to update an entry

我正在嘗試使用提交的表單插入數據。 數據在數據庫中正確輸入,但我也在為移動設備做 API。 如果收到is_api參數,我正在做jsone_encode 以下是代碼:

控制器:

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 我需要你的幫助來解決這個問題,謝謝!

檢查這個:

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
}

在您的版本中,有可能發送空數組 :) 這就是為什么顯示此異常的原因 :)

將您的$user_data['data'] = array更改$data['data'] = array

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM