在codeigniter模型中的查询似乎不起作用

时间:2016-02-20 12:56:11

标签: php sql codeigniter

我的模型中的此功能旨在验证学生用户是否插入了正确的凭据。我怀疑我的查询结构是否有问题。这是我的代码段:

index.php (查看)

            <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 (控制器)

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 (型号)

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()是我宣布的功能,所以不要介意。提前感谢那些可以帮助我的人。 :)

3 个答案:

答案 0 :(得分:1)

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;


    }

答案 1 :(得分:0)

试试此代码

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

答案 2 :(得分:0)

试试这段代码:

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