这是我的代码-
控制器
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!";
}
}
}
正如你所看到的,我特别添加了返回"耶"和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!";
}
}
重命名class name Categories to Categories_model
并命名file as categories_model.php
。您可能无法访问模型文件,因为您在模型文件中放置了错误的类名
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!";
}
}
}