微信小程序学生注册防止重复记录

学习微信小程序有一段时间了,今天想要总结一下微信小程序联合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'=>"添加出错"));
              }
       }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值