[eluser]zreedeecom[/eluser]
Ok.. I've cleaned a bit your code, please read it carefully and change the things you need to make it work with the rest of your code.
I've also uploaded the file with the code.
Code:
<?php
#MODEL
#Add a constructor to load the library
function __construct() {
parent::Model;
$this->load->database(); #I assume that you have your config file filled ok
} //end __construct
function validate() {
$u = $this->input->post('username');
$p = md5($this->input->post('password');
$this->db->where('username', $u)->where('password', $p); #Chaining method is cleaner
$q = $this->db->get('teacher');
if($q->num_rows > 0) {
$row = $query->row();
//Better than return one property ($row->tech_id) we gonna return the whole object
return $row;
}
else {
return false; //If no records found return false
}
}//end validate
#CONTROLLER
function validate_users() {
$this->form_validation->set_rules('username', 'Username', 'required|trim'); #why not add a min_legth?
$this->form_validation->set_rules('password', 'Password', 'required|trim'); #why not add a min_legth?
if( $this->form_validation->run() == false ) {
$this->index();
}
else {
$this->load->model('teachers_model');
$result = $this->teachers_model->validate();
if( $result == true ) {
// if the user’s credentials validated…
$data = array(
'tech_id' => $result->tech_id,
'username' => $result->username,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('main/teachers/memberarea'); //Is this link ok??
}
else {
//If the model has returned False show an error
//Maybe you can do something like...
$this->session->set_flashdata('error', 'Error: No users found');
$this->index();
}
}
}//end validate_users
#VIEW
#Put this where you have the form, is not the best way but it should do the work.
<?= ($this->session->flashdata('error')) ? $this->session->flashdata('error') : ''; ?>