php - 无法通过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>
解决方案
如果您在 URL 中发送数据,则通过 GET 发送数据。但是,您的代码正在尝试接收通过 POST 发送的数据。我认为在 codeigniter 中,您可以简单地编写如下代码以将其作为 GET 接收:
'sensor_id' => $this->input->get('sensor_id'),
'value' => $this->input->get('value')
推荐阅读
- google-cloud-storage - 什么是 gsutil 等价于 objects/get?
- css - IE11 中的 CSS hsl calc 错误
- c# - 统一按下移动设备中的后退按钮后,如何将输入的文本保存在输入字段中
- java - 如何删除windows上的临时文件?
- python - 为什么 Dask 执行得这么慢,而多处理执行得这么快?
- rest - 将 .zip 文件上传到 Sharepoint 的 Curl Post 请求返回“压缩(压缩)文件夹在下载时无效
- javascript - 除了在 CSP 策略中添加 'unsafe-inline' 以添加内联样式 attr 之外,是否有任何替代 setAttribute() 的方法?
- reactjs - React TypeScript:在同一元素的 2 个实例中设置状态会导致无限循环
- weblate - 启用 MT_SERVICE 会导致内部服务器错误
- excel - 文本框不填充基于组合框的值