Codeigniter:您必须使用“set”方法更新条目

时间:2016-06-24 18:08:56

标签: php mysql codeigniter api

我正在尝试使用提交的表单插入数据。数据在数据库中正确输入,但我也在为移动设备做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 。我需要你的帮助来解决这个问题,谢谢!

2 个答案:

答案 0 :(得分: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
}

在你的版本中,有可能发送空数组:)这就是为什么这个例外显示:)

答案 1 :(得分:0)

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