无法通过 rest api 插入数据

我尝试在 codeigniter 中使用 rest api 来插入数据。


我试试这个


    public function sensor_post(){

    $data = array(

                'sensor_id'   => $this->input->post('sensor_id'),

                'value'       => $this->input->post('value'));


    $insert = $this->db->insert('measurement', $data);


    if ($insert) {

        $this->response($data, 200);

    } else {

        $this->response(array('status' => 'fail', 502));

    }


  }

并调用 http://localhost/rest_iot/index.php/iot/sensor?sensor_id=1&value=37


我在邮递员中尝试这样做,但出现这样的错误。我想我已经填满了价值,为什么会这样?


   <h1>A Database Error Occurred</h1>

    <p>Error Number: 1048</p>

    <p>Column 'sensor_id' cannot be null</p>

    <p>INSERT INTO `measurement` (`sensor_id`, `value`) VALUES (NULL, NULL)</p>

    <p>Filename: C:/xampp/htdocs/rest_iot/system/database/DB_driver.php</p>

    <p>Line Number: 691</p>


慕雪6442864
浏览 97回答 3
3回答

米琪卡哇伊

如果您在 URL 中发送数据,则您是通过 GET 发送数据。但是,您的代码正在尝试接收通过 POST 发送的数据。我假设在 codeigniter 中您可以简单地编写如下代码以将其作为 GET 接收:&nbsp; &nbsp; &nbsp; &nbsp; 'sensor_id'&nbsp; &nbsp;=> $this->input->get('sensor_id'),&nbsp; &nbsp; &nbsp; &nbsp; 'value'&nbsp; &nbsp; &nbsp; &nbsp;=> $this->input->get('value')

阿晨1998

您是通过 GET 还是 POST 发送数据?假设$this->post('sensor_id')您正在寻找 POST 但通过 GET 发送sensor_id=1&value=37。通过 POST 方法发送数据或...通过 GET 检索

慕田峪4524236

您在帖子字段中缺少输入。public function sensor_post()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sensor_id'&nbsp; &nbsp;=> $this->input->post('sensor_id'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value'&nbsp; &nbsp; &nbsp; &nbsp;=> $this->input->post('value')&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $insert = $this->db->insert('measurement', $data);&nbsp; &nbsp; &nbsp; &nbsp; if ($insert) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response($data, 200);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->response(array('status' => 'fail', 502));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这对你有用。
打开App,查看更多内容
随时随地看视频慕课网APP