0

我创建了一个管理页面,用于创建帐户、删除帐户和更改用户密码。我对更改密码的编码有疑问。下面我附上使用codeigniter创建的代码。(不需要更改旧密码)

首页有这样的: 员工姓名: 新密码: 确认密码:

控制器文件。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Admin extends MY_Controller {

    public function index()     
    {       
      $this->load->view('view-admin'); 
    } 

    public function chgPassword()       
    {   
        $query => $this->modeluser->changpasswrd();
        $this->load->view('view-chg-password');
        $this->view-chg-password->set_rules(‘npassword’,'New Password’,'required|trim’);
        $this->view-chg-password->set_rules(‘cpassword’,
                           'Confirm Password’,'required|trim|matches[npassword]‘);

    $this->session->set_flashdata('message', '<span class="label label-info">Password changed!</span> ');
    redirect(base_url().'admin/chgpassword');       
    }
}

模型文件。

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ModelUser extends CI_Model {

public function changpasswrd($nama_staf, $password) {
 $this->db->set('password', $password);
  $this->db->where('nama-staf', $nama_staf);

  $this->db->update('akaun');
  return $this->db->affected_rows() > 0; }


function DeleteUser($options = array()) {
 // required values
 if(!$this->_required(array('userId'), $options)) return false;

 $this->db->where('userId', $options['userId']);
 $this->db->delete('users'); }

请帮助我。

4

1 回答 1

2

你应该使用form_validation

$this->form_validation->set_rules('npassword','New Password','required|trim');
$this->form_validation->set_rules('cpassword','Confirm
Password','required|trim|matches[npassword]');
if($this->form_validation->run() == FALSE){
    $this->session->set_flashdata('message', 
            '<span class="label label-info">Error! Password not changed!</span>');
    redirect(base_url().'admin/chgpassword');      
}
else{
    $query => $this->ModelUser->changpasswrd($this->input->post('nama-staf'),
                                              $this->input->post('npassword'));
    $this->session->set_flashdata('message', 
                   '<span class="label label-info">Password changed!</span>');
    redirect(base_url().'admin/chgpassword');
}

代替

$this->view-chg-password->set_rules('npassword','New Password','required|trim');
$this->view-chg-password->set_rules('cpassword','Confirm Password',
                      'required|trim|matches[npassword]');

加个construct functioncontroller喜欢的,

public function __construct(){
    parent::__construct();
    $this->load->model('ModelUser');
}

读取表单验证()

于 2013-08-06T04:41:05.540 回答