sdabrutas
sdabrutas

Reputation: 1517

Query in codeigniter model doesn't seem to work

This function in my model is intended to validate if the student user inserted a correct credential. I doubt it's the structure of my query that is problematic. Here's my code segment:

index.php (view)

            <form method="POST" action="<?php echo site_url('home/validateStud');?>" name="formvalidate">
              <div class="form-group">
                <input type="text" class="form-control" id="idnum" placeholder="ID Number"/>
                <br>
                <input type="password" class="form-control" id="pass" placeholder="PASSWORD"/>
                <br>
                <button type="submit" class="btn btn-primary" style="width: 100%;" onclick="" name="submit2" value="<?php echo $this->uri->uri_string(); ?>" id="validated">Validate</button>
              </div>
            </form>

home.php (controller)

public function validateStud()
{
  $submit=$this->input->post('submit2');
  $studentId=$this->input->post('idnum');
  $password=$this->input->post('pass');
  $this->load->model('StudentModel', 'Student', true);

  $valid=$this->Student->checkValidation($studentId, $password);
  if (isset($_POST['submit2']) && $valid) {
    redirect('student/index', true);
  }
  else
  {
    $indication = $this->initializeDialogBox('Record not found', ' Student Validation', 'fa-primary', 'fa-user', 'danger');
    $this->session->set_flashdata('message', $indication);
    redirect($this->input->post('submit2'));  
  }
}

studentmodel.php (model)

public function checkValidation($studentId, $password)
    {
        $query="SELECT * from STUDENT WHERE STUDENT_ID='".$studentId."' AND PASSWORD='".$password."'";
        $queryValid=$this->db->query($query)->row();

        if($queryValid->num_rows == 1)
        {
            return true;
        }
        else
            return false;
    }

initializeDialogBox() is a declared function by me, so don't mind it. Thanks in advance for those who can help me. :)

Upvotes: 0

Views: 118

Answers (3)

Praveen Kumar
Praveen Kumar

Reputation: 2408

public function checkValidation($studentId, $password)
    {
        $query = "SELECT * from STUDENT WHERE STUDENT_ID='".$studentId."' AND PASSWORD='".$password."'";
        $queryValid = $this->db->query($query)->row(); // it has data of that row

        if($this->db->query($query)->num_rows() > 0)
        {
            return true;
        }

        return false;


    }

Upvotes: 1

F. Furlanetti
F. Furlanetti

Reputation: 1

Try this code:

    $query = $this->db->get_where('STUDENT',array('STUDENT_ID'=>$studentId, PASSWORD'=>$password))->row();
    $num_rows = $this->db->get_where('STUDENT',array('STUDENT_ID'=>$studentId, PASSWORD'=>$password))->num_rows();

    if($num_rows>0){
       return true;
    }else{
       return false;
    }

Upvotes: 0

Arvind Jaiswal
Arvind Jaiswal

Reputation: 442

Try this code

public function checkValidation($studentId, $password)
{

    $q=$this->db->get_where('STUDENT',array('STUDENT_ID'=>$studentId,'PASSWORD'=>$password));

    if ($q->num_rows() > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Upvotes: 0

Related Questions