• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
details not being pull from databse table user login

#1
[eluser]learning_php[/eluser]
Hi,

I have been trying to create a user login but if i enter deatils that i know are in the databse table it siplays the error message?

Code:
<?PHP
class Login extends Controller{
    
    function login(){
        parent::Controller();
    }        
    
    function index(){
        if ($this->session->userdata('logged_in')== true)
        {
            redirect('dashboard');
        }
        $data['username'] = array('id'=> 'username');
        $data['password'] = array('id'=> 'password');
        $this->load->view('login',$data);
    }
    
    function process_login()
    {
        $this->db->get('members');
        $username = $this->db->select("username");
        $password = $this->db->select("password");
        
        if($username=='username' AND $password=='password')
        {
            $data = array('username'=>$username,'logged_in'=>true);
            $this->session->set_userdata($data);
            redirect('dashboard/index');
        
        }
        else{
                $this->session->set_flashdata('message','<div id="message">you have entered the wrong details</div>');
            redirect('login/index');
        }
    }
        function logout(){
        $this->session->sess_destroy();
        redirect('login/index');
        }
    
}
?&gt;

Code:
&lt;?PHP echo form_open('login/process_login') . "\n";?&gt;
    &lt;?PHP echo form_fieldset('login') . "\n";?&gt;
        
        &lt;?PHP echo $this->session->flashdata('message');?&gt;
        
        <p><label for="username"> Username: </label>&lt;?PHP echo form_input($username);?&gt;</p>
        <p><label for="password"> password: </label>&lt;?PHP echo form_password($password);?&gt;</p>
        <p>&lt;?PHP echo form_submit('login','login');?&gt;
        &lt;?PHP echo form_fieldset_close();?&gt;
    &lt;?PHP echo form_close();?&gt;

#2
[eluser]Dam1an[/eluser]
What error message do you get?

#3
[eluser]learning_php[/eluser]
Hi,

it not a code error it's skipping the first part of the if statement and displaying the else below

Code:
$this->session->set_flashdata('message','<div id="message">you have entered the wrong details</div>');

#4
[eluser]TheFuzzy0ne[/eluser]
Form inputs are submitted by name, not ID, so you need to add some name attributes to your input and password controls.

#5
[eluser]Dam1an[/eluser]
$this->db->select doesn't return anything, it just builds up the query
When setting the username and password, you need to do
Code:
$username = $this->input->post('username');
$password = $this->input->post('password');

Then attempt to get the row which matches these values from the database, if you get 1 result, its a match, else they entered a wrong username and password

#6
[eluser]TheFuzzy0ne[/eluser]
I agree. I didn't notice that, but you've got two calls to select which never get executed, and even if they did, they wouldn't work.

Code:
function is_valid_user($username, $password)
{
    $CI =& get_instance();
    $res = $CI->db->get_where('users', array('username' => $username, 'password' => $password));
    return ($res->num_rows() == 1);
}

#7
[eluser]learning_php[/eluser]
Hi,

I have adjusted the controller to this:

Code:
&lt;?PHP
class Login extends Controller{
    
    function login(){
        
        parent::Controller();
        
    }        
    
    function index(){
        
        if ($this->session->userdata('logged_in')== true)
        {
            redirect('dashboard');
        }
        $this->load->view('login');
    }
    
    function process_login()
    {
        
        $username = $this->input->post("username");
        $password = $this->input->post("password");
        
          $res = $CI->db->get_where('members', array('username' => $username, 'password' => $password));
        return ($res->num_rows() == 1);
        
        if($username=='username' AND $password=='password')
        {
            $data = array('username'=>$username,'logged_in'=>true);
            $this->session->set_userdata($data);
            redirect('dashboard');
    
    
        }
        else{
                $this->session->set_flashdata('message','<div id="message">you have entered the wrong details</div>');
            redirect('login/index');
        }
    }
        function logout(){
        $this->session->sess_destroy();
        redirect('login/index');
        }

}
?&gt;

and it give me this error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: CI

Filename: controllers/login.php

Line Number: 25
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/login.php

Line Number: 25

Fatal error: Call to a member function get_where() on a non-object in C:\xampp\htdocs\wedding-site\system\application\controllers\login.php on line 25

#8
[eluser]Dam1an[/eluser]
instead if $CI->, use $this->

You only need to use $CI (after assigning the result of get_instance() to it) in helpers and libraries

#9
[eluser]learning_php[/eluser]
Hi,

I dont seem to be getting an errors but i get taken to a blank sceen regardless if the login details are correct or not?

#10
[eluser]learning_php[/eluser]
Hi,

I have been putting a echo statements to see if it get printed out and it does before this line but not after.

Code:
return ($res->num_rows() == 1);

Does that mean this line is making the code fall over?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.