我创建了一个管理页面,用于创建帐户,删除帐户和更改用户密码。我有编码更改密码的问题。下面我附上使用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'); }
请帮我解决这个问题..
答案 0 :(得分: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 function
中添加controller
,
public function __construct(){
parent::__construct();
$this->load->model('ModelUser');
}