公共文件:\application\common.php
/**
* 检查添加标题、名称是否重复/或者查询结果 ,结束返回字符串 返回no表示得利,返回字符串是取得结果
* @param string $dataname [数据表名称]
* @param array $where [条件数组 比如:$where1['name'] = input('name'); $where1['id'] = input('id');]
* @param int $type [类型: 0=检查添加重复;1=查询得出结果]
* @param string $field [字段:比如:title]
*/
function checkRepeat($dataname,$where,$type,$field){
if($dataname){
$rsp=Db::name($dataname)->where($where)->find();
if($type){//1=查询得出结果
if($rsp){
return $rsp[$field];
}
}else{//0=检查添加重复
if($rsp){
return "no";
}
}
}
}
控制器中调用:
//判断是否重复添加
$where1['name'] = input('name');
$checkRepeat=checkRepeat('admin',$where1,0,'');
if($checkRepeat=="no"){
$this->error('错误:已经存在');
}