0

所以这是我的代码 -

控制器

public function addCategory() {
    if($this->session->userdata('logged_in') == TRUE) {
        $this->load->model("categories");
        echo $this->categories->addCategory();
    }
}

模型 -

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

class Categories extends CI_Model {

    // Retrieves all category names and id's
    function getCategories() {

    }

    // Add category to database. If there are errors returns message to AJAX controller
    function addCategory() {
        return "yeah";
        das
        $categoryName = $_POST['categoryName'];
        $parentCategory = $_POST["parentCategory"];
        $error = "";
        if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
            $query = $this->db->get_where("categories", array("name" => $categoryName));
            if($query->num_rows() > 0) {
                $error = "Category with that name already exists!";
            }
            else {
                $data = array(
                    'name' => $categoryName,
                    'parent' => $parentCategory
                );
                $this->db->insert('categories', $data);
            }
        }
        else {
            $error = "Category Name can't be empty.";
        }
        if($error != "") {
            return $error;
        }
        else {
            return "Success! Category has been added!";
        }
    }

}

正如你所看到的,我特别添加了 return "yeah" 和 das,看看它是否有效,但它没有,它只是返回空白页而没有错误。此外,这是 AJAX 请求,如果我在控制器中添加模型内容,它工作得很好。

可能是什么问题呢?如果您需要任何其他信息,请通知我。

编辑:同样,Das 只是我尝试过的东西,也没有显示任何错误,这是我当前的模型代码,没有未使用的条目 -

// Add category to database. If there are errors returns message to AJAX controller
function addCategory() {
    $categoryName = $_POST['categoryName'];
    $parentCategory = $_POST["parentCategory"];
    $error = "";
    if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
        $query = $this->db->get_where("categories", array("name" => $categoryName));
        if($query->num_rows() > 0) {
            $error = "Category with that name already exists!";
        }
        else {
            $data = array(
                'name' => $categoryName,
                'parent' => $parentCategory
            );
            $this->db->insert('categories', $data);
        }
    }
    else {
        $error = "Category Name can't be empty.";
    }
    if($error != "") {
        return $error;
    }
    else {
        return "Success! Category has been added!";
    }
}
4

2 回答 2

2

重命名class name Categories to Categories_model并命名。file as categories_model.php您可能无法访问模型文件,因为您在模型文件中输入了错误的类名

于 2012-08-04T07:10:24.530 回答
1

das 不被 PHP 识别,所以它默默地失败了unexpected T_VARIABLE

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

class Categories extends CI_Model {

    // Retrieves all category names and id's
    function getCategories() {

    }

    // Add category to database. If there are errors returns message to AJAX controller
    function addCategory() {
        return "yeah";
        $categoryName = $_POST['categoryName'];
        $parentCategory = $_POST["parentCategory"];
        $error = "";
        if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
            $query = $this->db->get_where("categories", array("name" => $categoryName));
            if($query->num_rows() > 0) {
                $error = "Category with that name already exists!";
            }
            else {
                $data = array(
                    'name' => $categoryName,
                    'parent' => $parentCategory
                );
                $this->db->insert('categories', $data);
            }
        }
        else {
            $error = "Category Name can't be empty.";
        }
        if($error != "") {
            return $error;
        }
        else {
            return "Success! Category has been added!";
        }
    }

}
于 2012-08-04T07:09:03.350 回答