如何在Codeigniter中验证现有用户或新用户

时间:2016-02-17 14:47:53

标签: forms codeigniter validation

我是初学者,我不知道如何使用codeigniter非常好所以我在这里问你们一些事情。这只是一个CRUD,我不知道如何使用Codeigniter验证现有用户/新用户。你们能帮助我吗?这是我的代码:

控制器:

class M_users extends CI_Controller {

        public function index()
        {
       if ($this->session->userdata('is_logged_in')){
              $this->load->helper('breadcrumb_helper');
            $data['data_get'] = $this->model_emp->view();
            $this->load->view('imports/head');
            $this->load->view('header');
            $this->load->view('musers', $data);
            $this->load->view('imports/footer');
            } else {
                redirect('home/restricted');
                
            }  
        }

        function add() {
          $this->load->view('header');
          $this->load->view('modals/addusers');
          $this->load->view('imports/footer');
        }
        function edit() {
          $kd = $this->uri->segment(3);
          if ($kd == NULL) {
            redirect('m_users');
          }
          $dt = $this->model_emp->edit($kd);
          $data['un'] = $dt->username;
          $data['em'] = $dt->email;
          $data['ps'] = $dt->position;
          $data['pd'] = $dt->password;
          $data['esm'] = $dt->emp_name;
          $data['gn'] = $dt->gender;
          $data['id'] = $kd;
          $this->load->helper('breadcrumb_helper');
          $this->load->view('imports/head');
          $this->load->view('header');
          $this->load->view('editusers', $data);
          $this->load->view('imports/footer');
        }
        function delete() {
          $u = $this->uri->segment(3);
          $this->model_emp->delete($u);
          $this->session->set_flashdata("message","<div class='alert alert-success alert-dismissable'>
            <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
            Successfully Deleted</div>");
          redirect('m_users');

        }
          
        
        function save() {
          if ($this->input->post('mit')){
            $this->model_emp->add();
            $this->session->set_flashdata("message","<div class='alert alert-success alert-dismissable'>
            <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
            Successfully Added</div>");
            redirect('m_users');
          } else{
            redirect('M_users/add');
          }
        }
        function update() {
          if ($this->input->post('mit')){
            $id = $this->input->post('id');
            $this->model_emp->update($id);
            $this->session->set_flashdata("message","<div class='alert alert-success alert-dismissable'>
            <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
            Successfully Updated</div>");
            redirect('m_users');
          } else{
            redirect('m_users/edit/'.$id);
          }
        }

      }

型号:

function add() {
    $un = $this->input->post('un');
    $em = $this->input->post('em');
    $ps = $this->input->post('ps');
    $pd = md5($this->input->post('pd'));
    $esm = $this->input->post('esm');
    $gn = $this->input->post('gn');
    $us = $this->input->post('us');
    $data = array (
      'username' => $un,
      'email' => $em,
      'position' => $ps,
      'password' => $pd,
      'emp_name' => $esm,
      'gender' => $gn,
      'usertype' => $us
      );
    $this->db->insert('employee', $data);
  }

  function view(){
    $take = $this->db->get('employee');
    if ($take->num_rows() > 0){
      foreach ($take->result() as $data) {
        $out[] = $data;
      }
      return $out;
    }
  }

  function edit($a) {
    $d = $this->db->get_where('employee', array('empID' => $a))->row();
    return $d;
  }

  function delete($a) {
    $this->db->delete('employee', array('empID' => $a));
    return;
  }

  function update($id) {
    $un = $this->input->post('un');
    $em = $this->input->post('em');
    $ps = $this->input->post('ps');
    $pd = md5($this->input->post('pd'));
    $esm = $this->input->post('esm');
    $gn = $this->input->post('gn');
    $us = $this->input->post('us');
    $data = array (
      'username' => $un,
      'email' => $em,
      'position' => $ps,
      'password' => $pd,
      'emp_name' => $esm,
      'gender' => $gn,
      'usertype' => $us
      );
    $this->db->where('empID', $id);
    $this->db->update('employee', $data);
  }
}

1 个答案:

答案 0 :(得分:1)

也许您可以尝试使用CRUD MY_Model: https://github.com/avenirer/CodeIgniter-MY_Model 它为数据库交互提供了完整的CRUD基础,以及基于事件的观察系统,智能表名称猜测和软删除。