0

Both variables are undefined($usuario and $senha)

This code is supposed to do login, but the model isn't receiving the variables.

Model

public function login()
{ 
    $query = $this->db->query("SELECT * FROM usuario WHERE login = '$usuario' AND senha = '$senha'");
    $query = $query->result();
    return $query;
}
3
  • 2
    Make sure you read up on variable scope Commented Oct 9, 2014 at 19:36
  • To format code, highlight the block, and click the {} button (not the "" button). Commented Oct 9, 2014 at 19:37
  • You need to pass $usuario and $senha to the login() function as parameters. Commented Oct 9, 2014 at 19:38

1 Answer 1

2

There's an indication from these two lines:

$this->Usuario_model->usuario = $this->input->post('usuario');
$this->Usuario_model->senha = sha1($this->input->post('senha'));

that there are variables inside of the Usuario-model that you have accessed and stored, but the login() function is trying to access variables inside of the scope of the function, not the scope of the model. Using $this-> will allow you to access the private, public, or protected variables of the class that the function lives in. Make sure you try:

public function login(){ 
    $query = $this->db->query("SELECT * FROM usuario WHERE login = '".$this->usuario."' AND senha = '".$this->senha."'");
    $query = $query->result();
    return $query;
}

To use a PDO-like approach, as per the CI docs, you should instead do:

public function login() {
    $query = $this->db->query(
        'SELECT * FROM usuario WHERE login = ? AND senha = ?',
        array($this->usuario, $this->senha)
    )
    $query = $query->result();
    return $query;
}

This will give you safer queries with escaped user values.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.