ThinkPHP实现对数据库的增删改查

好久都没有更新博客了,之前老师布置的任务总算是现在可以说告一段落了,今天趁老师还没提出其他要求来更新一篇博客。

今天我想记录的是我之前做项目,自己所理解的ThinkPHP对数据库的增删改查。

首先要说的是查,每一个app恐怕都少不了登录的功能吧。所以为了以防自己忘记,也为了那些曾经跟我一样不懂怎么实现登录的小伙伴们提供一下自己实现登录的后台代码。

登录功能需要客户端传用户名和密码,当服务器判断确实是数据库中真实存在的数据时就显示登录成功。否则返回登录失败。

//登录功能的实现
    public function test_login(){
        $spinnerId = I('spinnerId');
        $astno = I('astno');
        $astpasswd = md5(I('astpasswd'));
        if( $spinnerId == '学生'){
             $Student = M("Student"); // 实例化User对象
             $condition['sno'] = $astno;
             $condition['passwd'] = $astpasswd;
            // 把查询条件传入查询方法
            $RowCount = $Student->where($condition)->Count(); 
            if($RowCount>0){
                session(null);//清除之前的session,避免干扰
                session('sno',$sno);
                echo '学生登录成功';
            }
            else{
                echo '对不起,账号密码输入错误!';
                
            }


    
        }else if($spinnerId == '教师'){
            $Teacher = M("Teacher"); // 实例化User对象
            $condition['tno'] = $astno;
            $condition['tpasswd'] = $astpasswd;
            $RowCount = $Teacher->where($condition)->Count(); 
            if($RowCount>0){
                
                echo '老师登录成功';
            }else{
                echo '对不起,账号密码输入错误!';
            }    


            
        }else{
        
        echo '身份或者账号密码错误';
        }
    
    
    }

这个是我对数据库查询的时候,需要查询两个表,因为用到了数据库中的右连接所以把自己的代码粘在了这里。

//Android端老师查看自己的题目功能

    public function test_look(){
        $tno = I('exp_tno');
         $Experiment = M("Experiment");
        //通过老师工号查学号
        $exp_sno = $Experiment -> where(" exp_tno = '".$tno."'") -> getField('sno');
        
        if($tno != ''){
            $myModel = new ThinkModel();
            $result=$myModel->query("select g_experiment.* ,g_student.*  from g_student
                                        right join (select * from g_experiment where g_experiment.exp_tno = '$tno') g_experiment 
                                                on g_experiment.sno = g_student.sno;");
            echo json_encode($result);
        }else{
        
            
        }
    
    }
    //Android端老师查看自己的题目功能

    public function test_lookstu(){
        $tno = I('exp_tno');
        $id = I('id');
         $Experiment = M("Experiment");
        //通过老师工号查学号
        $exp_sno = $Experiment -> where(" exp_tno = '".$tno."'") -> getField('sno');
        //echo $tno;
        if($tno != ''){
            $myModel = new ThinkModel();
            $result=$myModel->query("select g_experiment.* ,g_student.*  from g_student
                                        right join (select * from g_experiment where g_experiment.exp_tno = '$tno' && g_experiment.id = '$id') g_experiment 
                                                on g_experiment.sno = g_student.sno;");
            echo json_encode($result);
        }else{
        
            
        }
    
    }

对数据库进行添加一条数据。

//添加选题
    public function test_add(){
        $exp_name = I('exp_name');
        $exp_tno = I('exp_tno');
        $exp_source = I('exp_source');
        $exp_type = I('exp_type');
        $exp_tech = I('exp_tech');
        $Experiment = M("Experiment");
        $condition['exp_name'] = $exp_name;
        $RowCount = $Experiment->where($condition)->Count(); 
        if($RowCount == 0){
            
        $data['exp_name'] = $exp_name;
        $data['exp_tno'] = $exp_tno;
        $data['exp_source'] = $exp_source;
        $data['exp_type'] = $exp_type;
        $data['exp_tech'] = $exp_tech;
        if($exp_tno != ''){
        $Experiment->add($data);
        echo '添加成功';
        }else{
        
            echo'添加失败';
        
        }
        }else{
            echo'题目重复,添加失败';
        }
        
    }
    

 删除数据库的某条数据

//删除选题
    public function test_delete(){
        
        $id = I('id');
        $Experiment = M("Experiment");
        if($id != ''){
        $Experiment->where(" id = '".$id."'")->delete();
        echo $id;
        echo '删除成功';
        }else{
        
        echo'删除失败';
        }
    }

 修改数据库中某条数据的信息

//修改个人信息
    public function test_message(){
        $tno = I('atsno');
        $tname = I('tname');
        $temail = I('temail');
        $tphone = I('tphone');
        $trole = I('trole');
        $Student = M("Student");
        $dta['tname'] = $tname;
        $data['tphone'] = $tphone;
        $data['trole'] = $trole;
        $data['temail'] = $temail;
        if($tno != ''){
        $Student -> where("tno = '".$tno."'") -> save($data);
        echo '修改信息成功';
        }else{
        echo '修改信息失败';
        }
    }
//修改密码
    public function test_repassword(){
        $tno = I('tno');
        $old_passwd = md5(I('old_passwd'));
        $new_passed1 = md5(I('new_passed1'));
        $new_passed2 = md5(I('new_passed2'));
        $Teacher = M('Teacher');
        $data['tpasswd'] = $new_passed1;
        $condition['tno'] = $tno;
        $RowCount = $Teacher->where($condition)->Count(); 
        if($RowCount == 1){
            $Teacher->where("tno = '".$tno."'")->save($data);

        echo '您的密码已经成功修改。vgfhd';
        }else{
        
        echo '老师端密码修改失败';
        }
    
    }
    //退选功能
    public function test_reselect(){
        if( 0 == C('CAN_SELECT')){
            echo '系统处于关闭状态';
        }else{
            $id= I('id');
            $Experiment = M("Experiment");
            $data['sno'] = '';
            $Experiment -> where("id = '".$id."'") -> save($data);
            echo '退选成功';
            
        }
    }
    //选题系统是否关闭
    public function test_android01(){
        //'CAN_SELECT'=>0, 1-能选题;0-禁止选题
        if( 0 == C('CAN_SELECT')){
            echo '系统处于关闭状态';
        }else{
            $this->test_android();
            
        }
    
    }

    //Android端查看全部没有被选的题目
    public function test_android(){
        
        $myModel = new ThinkModel();
        $result=$myModel->query("select id,exp_name,tname,exp_tech
                                from g_experiment,g_teacher 
                                where (g_experiment.sno = '' or sno is null)
                                    && g_experiment.exp_tno = g_teacher.tno;");
        
        echo json_encode($result);
    }
    //Android端查看我的选题
    public function test_check(){
     $sno = I('sno');
     $Experiment = M("Experiment");
    $exp_tno = $Experiment -> where("sno = '".$sno."'") -> getField('exp_tno');
    $RowCount = $Experiment -> where("sno = '".$sno."'") -> Count();
    if($RowCount == 1){
     $myModel = new ThinkModel();
        $result=$myModel->query("select *from g_experiment,g_teacher where sno = $sno && tno = $exp_tno ;");
        $this ->ajaxReturn($result);
    }else{
    echo '你还没有选题';
    }
    }
    //修改信息
    public function test_reset(){
     $sno = I('sno');
     $Student = M("Student");
    $RowCount = $Student -> where("sno = '".$sno."'") -> Count();
    if($RowCount == 1){
     $myModel = new ThinkModel();
        $result=$myModel->query("select *from g_student where sno = $sno;");
        $this ->ajaxReturn($result);
    }
    }


    //退选功能
    public function test_reselect(){
        if( 0 == C('CAN_SELECT')){
            echo '系统处于关闭状态';
        }else{
            $id= I('id');
            $Experiment = M("Experiment");
            $data['sno'] = '';
            $Experiment -> where("id = '".$id."'") -> save($data);
            echo '退选成功';
            
        }
    }
    //修改密码
    public function test_repassword(){
        $sno = I('sno');
        $old_passwd = md5(I('old_passwd'));
        $new_passed1 = md5(I('new_passed1'));
        $new_passed2 = md5(I('new_passed2'));
        $Student = M('Student');
        $data['passwd'] = $new_passed1;
        $condition['sno'] = $sno;
        $RowCount = $Student->where($condition)->Count(); 
        if($RowCount == 1){
            $Student->where("sno = '".$sno."'")->save($data);

        echo '您的密码已经成功修改。';
        }else{
        
        echo '老师端密码修改失败';
        }
    
    }
    /*选题功能
    思想:拿到题号查询学号是否为空,若为空则证明该题未选否则显示该题已经选过了
          为空的情况下拿着学号查询题号是否为空,若为空则证明该学生尚未选题可进行选题
          否则需要退选之后选题。
    */
    public function test_select(){
        $Experiment = M("Experiment");
        $id= I('id');
        $sno = I('sno');
        
        //通过id查找学号
        $exp_sno = $Experiment -> where("id = '".$id."'") -> getField('sno');
        if($exp_sno != ''){
        echo '该题已经被选';
        }else{
        //通过学号查找id
        $exp_id = $Experiment -> where("sno = '".$sno."'") -> getField('id');
        if($exp_id != null){
        echo '需要退选之后才能选题';
        }else if($sno != ''){
            $data['sno'] = $sno;
            $Experiment -> where("id = '".$id."'") -> save($data);
            echo '选题成功';
        }
        }
    }
//搜索功能
    public function test_search(){
        $Experiment = M("Experiment");
        $Teacher = M("Teacher"); 
        $tname = I('tname');
        $exp_tno = $Teacher -> where("tname = '".$tname."'") -> getField('tno');
         $myModel = new ThinkModel();
        $result=$myModel->query("select id,exp_name,istate from g_experiment where exp_tno = $exp_tno;");
        echo json_encode($result);
    
    }
    //完善信息
    public function test_message(){
        $sno = I('sno');
        $cellphone = I('cellphone');
        $email = I('email');
        $qq = I('qq');
        $Student = M("Student");
        $data['cellphone'] = $cellphone;
        $data['email'] = $email;
        $data['qq'] = $qq;
        $Student -> where("sno = '".$sno."'") -> save($data);
        echo '完善信息成功';
    }
    
    //模糊搜索功能
    public function test_search02(){
        if(0 == C('CAN_SELECT')){
            echo '系统处于关闭状态';
        }else{
        $Experiment = M("Experiment");
        $Teacher = M("Teacher"); 
        $tname = I('exp_tname');
        $exp_tno = $Teacher -> where("tname = '".$tname."'") -> getField('tno');
        $myModel = new ThinkModel();
        $result=$myModel->query("select id,exp_name,tname 
                                from g_experiment,g_teacher 
                                where (exp_name LIKE '%$tname%' OR exp_tno = '$exp_tno') AND g_experiment.exp_tno = g_teacher.tno;");
        echo json_encode($result);
        }
    }
原文地址:https://www.cnblogs.com/kangyaping/p/5998133.html