简体   繁体   English

在Codeigniter中使用Ajax登录(控制器和模型无法执行查询数据库)

[英]Login with Ajax in Codeigniter (Controllers and Models can not execute Query Database)

I have a View that will send Login data with Ajax jquery as follows : views/Login.php 我有一个视图,将使用Ajax jQuery发送登录数据,如下所示:views / Login.php

 I have a View that will send Login data with Ajax jquery as follows : views/login.php <!-- begin snippet: js hide: false console: true babel: false --> 

That will send username & password to Model Login / insert_login controller/Login.php 这会将用户名和密码发送到Model Login / insert_login controller / Login.php

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public function __construct () { parent::__construct(); $this->load->model('Login_Model'); } public function index () { $this->load->view('Login'); } public function insert_login () { $user = $this->input->post('user'); $pass = $this->input->post('pass'); $cekUser = $this->Login_Model->check_user($user); if (empty($cekUser->username)) { $sts = 'user_wrong'; }else{ if (md5($pass) != $cekUser->password) { $sts = 'pass_wrong'; }else{ if ($cekUser->tipe_user == 'admin'){ $sts = 'success_admin'; $this->session->set_userdata(array('user'=>$user,'status'=>'admin')); redirect(base_url('Admin/Home')); }else{ $sts = 'success_user'; $this->session->set_userdata(array('user'=>$user,'status'=>'user')); redirect(base_url('Admin/Home')); } } } echo '{"status":"'.$user.'"}'; } public function logout () { $this->session->sess_destroy(); redirect(base_url('Admin/Login')); } } ?> <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login_Model extends CI_Model { public function check_user ($user) { return $this->db->get_where('m_user', array('username' => $user))->row(); } } ?> 

I wonder why Query Database Function get_where / query / Any query can not be executed, but I have added Liblary Database in autoload.php: $autoload['libraries'] = array('database','session'), And I test using a Controller other than Login Controller still can run. 我不知道为什么查询数据库功能get_where / query /任何查询都无法执行,但是我在autoload.php中添加了Liblary数据库:$ autoload ['libraries'] = array('database','session'),然后我测试使用登录控制器以外的控制器仍可以运行。 Why only in the model & Login controller can not execute Query? 为什么仅在模型和登录控制器中无法执行查询?

But if I try to display the data captured divariabel: 但是,如果我尝试显示divariabel捕获的数据:

  $user = $this->input->post('user'); $pass = $this->input->post('pass'); echo $user.' '.pass; 

When i test like it can already get and display data, tapi masalahnya kenapa ketika memunculkan Query tidak muncul apapun. 当我测试可以获取并显示数据时,请点击查询tiap muncul apapun。 Please help me, I've really stack, stay up every day to clean up my job ... 请帮助我,我真的很累,每天熬夜清理我的工作...

set data into array in controller file 将数据设置为控制器文件中的数组

set name of array key as your database field name username & password 将数组键的名称设置为数据库字段名称usernamepassword

controller file 控制器文件

  $user = $this->input->post('user');
  $pass = $this->input->post('pass');
  $where=array('username'=>$user,
            'password'=>md5($pass));
  $cekUser = $this->Login_Model->check_user($where);

give yout table name in from query 在给YOUT表名from查询

model file 模型文件

class Login_Model extends CI_Model {

  public function check_user ($where)
  {
    $this->db->select('*');
    $this->db->from('table_name');
    $this->db->where($where);
    $res=$this->db->get();
    return $res->result();
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM