gpt4 book ai didi

php - 消息:未定义的属性:Users::$form_validation

转载 作者:行者123 更新时间:2023-12-01 23:08:13 26 4
gpt4 key购买 nike

我目前正在尝试将我的应用程序从 MySQL 数据库更改为 MongoDB 数据库。我的项目需要比较其中的两个,而我的 MySQL 代码正在运行,现在它给了我这个错误。

堆栈跟踪:

A PHP Error was encountered Severity: Notice Message: Undefined property: Users::$form_validation Filename: controllers/users.php Line Number: 26

错误行:

$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');   

代码:

<?php
class Users extends CI_Controller{
public function register(){
$this->form_validation->set_rules('first_name','First Name','trim|required|max_length[50]|min_length[2]|xss_clean');
$this->form_validation->set_rules('last_name','Last Name','trim|required|max_length[50]|min_length[2]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|max_length[100]|min_length[5]|xss_clean|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|max_length20]|min_length[6]|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|max_length[20]|min_length[6]|xss_clean');
$this->form_validation->set_rules('password2','Confirm Password','trim|required|max_length[50]|min_length[2]|xss_clean|matches[password]');
if($this->form_validation->run() == FALSE){
$data['main_content']='users/register';
$this->load->view('layouts/main',$data);
}else{
if($this->User_model->create_member()){
$this->session->set_flashdata('registered','Registered successfully');
redirect('home/index');
}
}
}

public function login(){
//$this->load->model('user_model');
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean');

$collection = $this->database->users;

if($this->form_validation->run() == FALSE){

$this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
redirect('home/index');
} else {

$username=$_POST['login'];
$password=$_POST['password'];

$user_id = $collection->findOne(array('username' => $username, 'password' => md5($password)));
// $username = $this->input->post('username');
// $password = $this->input->post('password');


$user_id = $this->User_model->login_user($username,$password);


if($user_id){

$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);

$this->session->set_userdata($user_data);

redirect('home/index');
} else {

$this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
redirect('home/index');
}
}
}

public function logout(){

$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('username');
$this->session->sess_destroy();

redirect('home/index');
}
}

最佳答案

更改 config/autoload.php 对我有用,只需在自动加载中添加 form_validation 即可:

$autoload['libraries'] = array('form_validation');

关于php - 消息:未定义的属性:Users::$form_validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com