学习微信小程序有一段时间了,今天想要总结一下微信小程序联合tp框架如何的查找有重复记录,阻止添加。
微信小程序在注册学生信息时,决定会出现学号重复,所以呢我们就根据学号来查找是否有相同的记录,首先呢,先是我们小程序的注册页面,如下
在输入学号的时候呢,如果库中有重复的记录,那么就不让它添加,如下图
这是怎样实现的呢,下面我们来看看,在js中呢,调用tp的借口
formSubmit: function (e) {
wx.request({
url: '',
data: e.detail.value,
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success:(res)=> {
console.log(res);
if(res.data.error){
wx.showToast({
title:res.data.msg,
icon:'none',
duration:2000
})
}else{
wx.showToast({
title:'添加成功',
icon:'none',
duration:2000
})
wx.navigateTo({
url: '../add/add',
})
}
}
})
},
在tp框架中,写入防止记录重复的代码:
public function add($no,$name,$sex,$age){
$data['no'] = $no;
$data['name'] = $name;
$data['sex'] = $sex;
$data['age']= $age;
$id = M('student');
if($id->where("no='{$no}'")->find()){
return $this->ajaxReturn(array('error'=>true,'msg'=>'学号重复'));
}else{
$id = $id->add($data);
if($id){
return $this->ajaxReturn(array('error'=>false,'id'=>$id));
}else{
return $this->ajaxReturn(array('error'=>true,'msg'=>"添加出错"));
}
}
}