目前使用CodeIgniter Framework我有一个错误

时间:2018-05-15 07:14:26

标签: php codeigniter-3

  

遇到PHP错误

     

严重性:注意

     

消息:未定义属性:Login :: $ U_Model

     

文件名:controllers / Login.php

     

行号:24

回溯:

  

文件:C:\ xampp \ htdocs \ Cmsproject \ application \ controllers \ Login.php

     

行:24

     

功能:_error_handler

     

文件:C:\ xampp \ htdocs \ Cmsproject \ index.php

     

行:315

     

功能:require_once

     

$ data = $ this-> U_Model-> loginf($ loginf ['用户名'],$ loginf ['密码']);

     

遇到未捕获的异常

     

输入:错误

     

消息:在null

上调用成员函数loginf()      

文件名:C:\ xampp \ htdocs \ Cmsproject \ application \ controllers \ Login.php

     

行号:24

回溯:

  

文件:C:\ xampp \ htdocs \ Cmsproject \ index.php

     

行:315

     

功能:require_once

模型文件在这里:

<?php
class U_Model extends CI_Model
{
    public function loginf($username,$pass){
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('username',$username);
        $this->db->where('password',$pass);
        if($query=$this->db->get()){
            return $query->row_array();
        }else{
            return false;
        }
    }
}
?>

2 个答案:

答案 0 :(得分:0)

希望这会对您有所帮助:

你的控制器应该是这样的:

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

class Login extends CI_Controller {
    public function __construct(){
        parent::__construct();
        //$this->load->model('User');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->database();
        $this->load->model('U_Model');
    }
    public function index(){
        $this->load->view('header');
        $this->load->view('login');
        $this->load->view('footer');

    }

    /*added public before loginp*/

    public function loginp(){
        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));
        /*md5 not recommended*/

        $data = $this->U_Model->loginf($username, $password);

        if( ! empty($data))
        {
            $this->session->set_userdata('sid',$data['id']);
            $this->session->set_userdata('sname',$data['name']);
            $this->session->set_userdata('username',$data['username']);
            redirect('login/home');
        }else{
            $this->session->set_flashdata('emsg','Please Enter Your Correct Name And Password');
            redirect('login/home');
        }
    }

    public function home(){
        $this->load->view('home');
    }
}?>

像这样改变你的模型

<?php
class U_Model extends CI_Model
{
    public function loginf($username,$pass)
    {
        $this->db->where('username',$username);
        $this->db->where('password',$pass);
        $query = $this->db->get('users')
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }
    }
}
?>

答案 1 :(得分:0)

<?php
class U_Model extends CI_Model
{
    public function loginf($username,$pass)
    {
        $this->db->where('username',$username);
        $this->db->where('password',$pass);
        $query = $this->db->get('users')
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }
    }
}
?>