CodeIgniter:无法从数据库中获取数据

时间:2015-05-08 07:07:14

标签: php codeigniter

这是我的观点:alumni / loginalumni.php

<form action="<?=site_url('controller_alumni/login/submit')?>" method="post">
Username: <input type="text" name="username"/>
Password: <input type="password" name="pass"/>
<input type="submit"  /></form>

这是我的控制器:controller_alumni.php

public function login($submit = null){

        if($submit==null){
            $this->load->view('alumni/login_alumni');
            return true;
        }
        $username=  $this->input->post('username');
        $pass=  $this->input->post('pass');

        $this->load->model('model_alumni');
        $result = $this->model_alumni->login($username, $pass);

        if($result==true){
            echo 'LogIn';
        }
        else{
            echo 'LogIn Failed';
        }                 
    }

这是我的模特:model_alumni.php

public function login($username, $pass){

    $query = $this->db->get_where('tb_alumni',[
        'username'          => $username,
        'pass'              => sha1($pass . HASH_KEY)
    ]);

    return $query->result();
} 

当我跑,然后我插入用户名和密码,它说:“登录失败”

2 个答案:

答案 0 :(得分:0)

请更改

public function login($submit = null){

    if($submit==null){
        $this->load->view('alumni/login_alumni');
        return true;
    }
    $username=  $this->input->post('username');
    $pass=  $this->input->post('pass');

    $this->load->model('model_alumni');
    $result = $this->model_alumni->login($username, $pass);

    if($result==true){
        echo 'LogIn';
    }
    else{
        echo 'LogIn Failed';
    }                 
}

 public function login($submit = null){

    if($submit==null){
        $this->load->view('alumni/login_alumni');
        return true;
    }
    $username=  $this->input->post('username');
    $pass=  $this->input->post('pass');

    $this->load->model('model_alumni');
    $result = $this->model_alumni->login($username, $pass);

    if(!empty($result)){
        echo 'LogIn';
    }
    else{
        echo 'LogIn Failed';
    }                 
}

因为如果你的用户名和密码是正确的,那么它的返回数据不是真的,$ result得到那些数据。所以请通过empty()

来检查

答案 1 :(得分:0)

so the problem is on my CREATE TABLE `user_passports` ( `id` int(11) NOT NULL AUTO_INCREMENT, `method` varchar(255) DEFAULT NULL, `token` varchar(255) DEFAULT NULL, `social_id` varchar(255) DEFAULT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `user_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), CONSTRAINT `user_passports_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; , , i dont know how to explain it, but when i delete method its work !!

thank you for mr. https://stackoverflow.com/users/3555774/rishi for your clue ,, thank you so much , , ,