php - 使用 CodeIgniter 检查字段值是否唯一
问题描述
我正在尝试检查添加到数据库中的值是否是unique.
这是我正在使用的代码,但即使我知道该号码已经在数据库中,也in_array()
不会返回。true
我是误会result_array()
还是something?
//Check if Serial is Already in Database
$this->db->select('fldUserSensorID');
$sensorqry = $this->db->get("tblMonitoringSensors");
$result = $sensorqry->result_array();
if (in_array($this->input->post('serialNumber'), $result)) {
$attempt=false;
}
解决方案
您正在使用result_array()
应该有这样的数据Array
: -
Array(
0=>Array("fldUserSensorID"=>1),
1=>Array("fldUserSensorID"=>3),
2=>Array("fldUserSensorID"=>9)
)
所以你应该试试这个:
$optimized_result=array_column($result, 'fldUserSensorID');
if (in_array($this->input->post('serialNumber'), $optimized_result)) {
$attempt=false;
}
推荐阅读
- html - Ionic5 组件获取 @ViewChild 不起作用
- haskell - Haskell - 如何基于 BinaryTree 的文件夹创建 mapTree 函数?
- python - 我的代码在 Python 中运行良好,如何从 HTML 传递参数值?
- java - Android视图模型类没有零参数构造函数,片段中的实例化异常
- python - 如何将保存在表单中的用户变量传递给 Django 中的按钮链接
- javascript - 如何根据下拉选择填充强类型文本框
- firebase - 为什么提供类“QueryDocumentSnapshot”的 Firestore 数据没有实例方法“[]”。错误?
- python - 路径('',views.index,name='index'),AttributeError:模块'learning_logs.views'没有属性'index'
- python - 如何在 Matplotlib 中绘制一个非常大的数据集(日期、时间(x 轴)与记录的值(y 轴),每天每 15 分钟一次,持续一年)
- javascript - 如何将 JSON 数据从前端 JavaScript 发送到使用 Python Flask 构建的后端服务器