2016-02-17 54 views
0

我是一名初學者,我不知道如何使用codeigniter,所以我在這裏問你們一些東西。這只是一個CRUD,我不知道如何驗證使用Codeigniter的現有用戶/新用戶。你們能幫我嗎?這裏是我的代碼:如何在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); 
 
    } 
 
}

+0

你是什麼意思的'驗證新用戶/現有用戶'?您是否想驗證登錄詳細信息?你想驗證字段?你需要驗證什麼? – Kisaragi

回答