0

感谢您阅读我的帖子,我在 nettus 教程和 codeigniter 上遇到了这个问题,我正在使用登录表单控制器和模型,并且我进行了更改以在用户登录时保持级别或角色,这里是错误

错误代码点火器

这是我的控制器(login.php)

    <?php
class Login extends CI_Controller {

    function index()
    {
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);
    }

    function validate_credentials()
    {
        $this->load->model('membership_model'); 
        $query = $this->membership_model->validate();

        if ($query) //Existe el usuario
        {
            $rol = $this->membership_model->roles($this->input->post('username')); //Busco el rol en el modelo
            $data = array(
                'username' => $this->input->post('username'),
                'rol' => $rol,
                'is_logged_in' => true
            );  

            $this->session->set_userdata($data);
            redirect('site/members_area');
        }
        else 
        {
            $this->index(); 
        }
    }

    function signup()
    {
        $data['main_content'] = 'signup_form';
        $this->load->view('includes/template', $data);
    }

    function create_member()
    {
        $this->load->library('form_validation');
        //field name, error message, validation rules

        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');   

        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_lenght[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_lenght[4]|max_lenght[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|min_lenght[4]|max_lenght[32]|matches[password]');

        if (($this->form_validation->run()) == FALSE)
        {
            $this->signup();
        }
        else 
        {
            $this->load->model('membership_model'); 
            if ($query = $this->membership_model->create_member)
            {
                $data['main_content'] = 'signup_succesful';
                $this->load->view('includes/template', $data);
            }
            else
            {
                $this->load->view('signup_form');   
            }
        }
    }
}
?>

这是我的模型(memebership_model.php)

<?php
class Membership_model extends CI_Model {

    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('membership');

        if($query->num_rows == 1)
        {
            return true;    
        }
    }

    function roles($usuario)
    {
        $sql = "SELECT roles FROM membership WHERE username = ? LIMIT 1";
        $q = $this->db->query($sql, $usuario);  
        if ($q->num_rows() > 0)
        {
            foreach($q->result() as $row) {
                $data = $row;
            }
            return $data;
        }
    }

    function create_member()
    {
        $new_member_insert_data = array(
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password')),
            'email_address' => $this->input->post('email_address'),
            'roles' => '1'
        );  

        $insert = $this->db->insert('membership', $new_member_insert_data);
        return $insert;
    }

}
?>

这是我执行时显示错误的视图(signup_form.php)

<h1>Create an Account</h1>
<fieldset>
    <legend>Personal Information</legend>
    <?php
        echo form_open('login/create_member');
        echo form_input('first_name', set_value('first_name', 'First Name'));
        echo form_input('last_name', set_value('last_name', 'Last Name'));
        echo form_input('email_address', set_value('email_address', 'Email Address'));
    ?>
</fieldset>
<fieldset>
    <legend>Login Info</legend>
    <?php
        echo form_input('username', set_value('username', 'Username'));
        echo form_input('password', set_value('password', 'Password'));
        echo form_input('password2', set_value('password2', 'Password Confirm'));

        echo form_submit('submit','Create Account');
    ?>

    <?php echo validation_errors("<p class='error'>"); ?>
</fieldset>
4

1 回答 1

0

CI_Controller 需要调用。

class Membership_model extends CI_Model {

之后添加

function __construct(){
    parent::__construct();
}
于 2013-05-24T08:39:39.457 回答