在控制器类上加载模型时出现500错误

时间:2014-01-31 07:23:03

标签: php codeigniter codeigniter-2 web-deployment

我正在使用Codeigniter 2.1.4,也是这个框架的新手。当我试图在控制器类上加载模型时,浏览器抛出错误ID 500。以下是模型类文件名的代码:

merchant_login.php

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

          class Merchant_model extends MY_Model {
                 public $tbl_user = 'users'; 
                 function __construct(){
                 parent::__construct();     
                 $this->load->database('merchant', TRUE, TRUE);    
                 }

          function user_login($data)
            {
                $this->db->from($this->tbl_user);
                $this->db->where($data);
                $query = $this->db->get();
                if($query->num_rows()==1)
                {
                    $this->db->select("CURRENT_LOGIN");
                    $this->db->from($this->tbl_user);
                    $this->db->where("USER_ID", $data["USER_ID"]);
                    $q = $this->db->get();
                    $res = $q->result();
                    $current_time = $res[0]->CURRENT_LOGIN; 
                    $last_time = $current_time;
                    $current_time = date("Y-m-j H:i:s");
                    $arr = array(
                        "CURRENT_LOGIN" => $current_time,
                        "LAST_LOGIN" => $last_time,
                        "LAST_IP" => $this->input->ip_address()
                    );

                    $this->db->where("USER_ID", $data["USER_ID"]);
                    $this->db->update($this->tbl_user, $arr);
                    $this->db->from($this->tbl_user);
                    $this->db->where($data);
                    $query = $this->db->get();
                    return $query->row_array();
                }
                return false;
               }
        } ?>

以下是控制器的代码,文件名为:

“的login.php”

    <?php
    if (!defined('BASEPATH')) exit('No direct script access allowed');
    class Login extends MY_Controller {

        function __construct() {
         parent::__construct();

            $this->load->library(array("form_validation", "session"));
        }
    //merchant section start
        public function merchant_login() {
            $this->session->keep_flashdata("return_url");
            $this->form_validation->set_rules('loginName', 'Login', 'trim|valid_email|required');
            $this->form_validation->set_rules('loginPassword', 'Password', 'trim|required');

            if ($this->session->userdata("merchantLogged") != 1) {
                if ($this->form_validation->run() == FALSE) {
                    $data["error"] = validation_errors();
                    header('Content-type: application/json');
                    echo json_encode($data);
                } else {

                    $d['USER_ID'] = $this->input->post("loginName");
                    $d['USER_PASSWD'] = sha1($this->input->post("loginPassword"));
                    $this->load->model("merchant_model", "model", TRUE);
                    $user = $this->model->user_login($d);
                    if ($user) {
                        if ($this->input->post("security") == 'not') {
                            //Cal to a member function of the model class
                            if ($this->model->user_ip_check($d) == false) {
                                $this->session->set_userdata('u_id', $this->input->post("loginName"));
                                $this->session->set_userdata('u_pd', $this->input->post("loginPassword"));
                                $data['redirect'] = site_url("merchants/ip_security/");
                            } else {
                                $usr['type'] = "MR";
                                unset($user['USER_PASSWD']);
                                //                  unset($user['ID']);
                                unset($user['MM_NAME']);
                                //$this->load->database('default', TRUE, TRUE);
                                $this->session->set_userdata($usr);
                                $this->session->set_userdata($user);
                                $this->set_merchantLogged(1);
                                //                      if($this->session->flashdata("return_url")){
                                //                          $data['redirect'] = site_url("user/" . $this->session->flashdata("return_url"));
                                //                      }
                                //                      else{

                                $data['redirect'] = site_url("merchant/account");
                                //                      }
                            }
                        } else {
                            $d['que'] = $this->input->post("sec_q");
                            $d['ans'] = $this->input->post("sec_q_answer");
                            if ($this->model->check_que($d) == true) {
                                $this->session->unset_userdata('u_id');
                                $this->session->unset_userdata('u_pd');
                                $usr['type'] = "MR";
                                unset($user['USER_PASSWD']);
                                unset($user['MM_NAME']);
                                $this->session->set_userdata($usr);
                                $this->session->set_userdata($user);
                                $this->set_merchantLogged(1);
                                $data['redirect'] = site_url("merchant/account");
                            } else {

                                $data['redirect'] = site_url("merchants/ip_security");
                            }
                        }
                    } else {
                        $data["error"] = "Username or password are wrong";
                        $data["islogin"] = true;
                    }
                    header('Content-type: application/json');
                    echo json_encode($data);
                }
            } else {
                if ($this->session->flashdata("return_url")) {
                    $data['redirect'] = site_url("merchant/" . $this->session->flashdata("return_url"));
                } else {
                    $data['redirect'] = site_url("home");
                }
                header('Content-type: application/json');
                echo json_encode($data);
            }
        }
    }
    ?>

2 个答案:

答案 0 :(得分:0)

将您的模型文件名从 merchant_login.php 更改为 merchant_model.php

并更改login.php

$this->load->model("merchant_model", "model", TRUE);

$this->load->model("merchant_model", "", TRUE);

答案 1 :(得分:0)

 class Merchant_model extends MY_Model  

替换

 class Merchant_model extends CI_Model


 class Login extends MY_Controller 

替换
     class Login扩展了CI_Controller

语法错误:$ this-&gt; load-&gt;数据库('merchant',TRUE,TRUE);纠正这个

我的建议请仔细阅读它可以帮助您的codeigniter文档 http://ellislab.com/codeigniter/user-guide/general/controllers.html