为了账号安全,请及时绑定邮箱和手机立即绑定

无法通过 rest api 插入数据

无法通过 rest api 插入数据

PHP
慕雪6442864 2022-12-23 13:27:22
我尝试在 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>
查看完整描述

3 回答

?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

如果您在 URL 中发送数据,则您是通过 GET 发送数据。但是,您的代码正在尝试接收通过 POST 发送的数据。我假设在 codeigniter 中您可以简单地编写如下代码以将其作为 GET 接收:


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

        'value'       => $this->input->get('value')


查看完整回答
反对 回复 2022-12-23
?
阿晨1998

TA贡献2037条经验 获得超6个赞

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

通过 POST 方法发送数据或...通过 GET 检索


查看完整回答
反对 回复 2022-12-23
?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

您在帖子字段中缺少输入。


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));

        }

    }

这对你有用。


查看完整回答
反对 回复 2022-12-23
  • 3 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信