php - Codeigniter:您必须使用 "set"方法来更新条目

标签 php mysql codeigniter api

我正在尝试使用提交的表单插入数据。数据已正确输入数据库,但我也在为移动设备提供 API。如果收到 is_api 参数,我将执行 jsone_encode。代码如下:

Controller :

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 时,我收到此错误和响应:

A Database Error Occurred

You must use the "set" method to update an entry.

Filename: /home/foldername/public_html/parcel/models/user_model.php

Line Number: 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
}

在您的版本中,有可能发送空数组:),这就是为什么显示此异常:)

关于php - Codeigniter:您必须使用 "set"方法来更新条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38019640/

相关文章:

Mysql NOT IN 子句查询不返回我想要的

c# - 所请求的名称有效,但未找到所请求类型的数据

php - 错误级别和哪一行的错误?

php - 如何将日期时间拆分为多个部分以用于重复行

php - 在 PHP 中搜索和统计数据

javascript - jQuery 无法在动态生成的按钮或 p 标签上运行 .click 事件

php - 调用与 javascript 函数位于不同文件中的 php 函数来从数据库检索数据

php - 如何使滚动条出现在div的边框内

PHP是否有数据库分类检索表

php - 如果存储桶尚不存在则创建它