所以这是我的代码 -
控制器
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!";
}
}