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

时间:2022-10-06 12:42:41

this is my views : alumni/loginalumni.php

这是我的观点: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>

this is my controller : controller_alumni.php

这是我的控制器: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';
        }                 
    }

and this is my model: model_alumni.php

这是我的模型: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();
} 

when i run, and then i insert username and password and it say : "LogIn Failed"

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

2 个解决方案

#1


Please change

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';
    }                 
}

To

 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';
    }                 
}

Because if your user name and password is correct then it's return data not true and $result get that data . so please check this by empty()

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

#2


so the problem is on my sha1($pass . HASH_KEY) , , i dont know how to explain it, but when i delete sha1() its work !!

所以问题出在我的sha1($ pass.HASH_KEY)上,我不知道如何解释它,但当我删除sha1()它的工作!

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

谢谢先生。 https://*.com/users/3555774/rishi为您提供线索,非常感谢,

#1


Please change

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';
    }                 
}

To

 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';
    }                 
}

Because if your user name and password is correct then it's return data not true and $result get that data . so please check this by empty()

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

#2


so the problem is on my sha1($pass . HASH_KEY) , , i dont know how to explain it, but when i delete sha1() its work !!

所以问题出在我的sha1($ pass.HASH_KEY)上,我不知道如何解释它,但当我删除sha1()它的工作!

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

谢谢先生。 https://*.com/users/3555774/rishi为您提供线索,非常感谢,