Php codeigniter模型方法不起作用

Php codeigniter模型方法不起作用,php,function,codeigniter,model,load,Php,Function,Codeigniter,Model,Load,我一直在学习教程 我一直看到3:50,它似乎不起作用 注意:我的路由工作正常,我在$autoload中调用了库,db工作正常,只是 controllers/admin.php <?php class Admin extends CI_Controller { public function index() { $this->load->view('admin'); } public function create_user() {

我一直在学习教程 我一直看到3:50,它似乎不起作用

注意:我的路由工作正常,我在$autoload中调用了库,db工作正常,只是

controllers/admin.php

<?php
class Admin extends CI_Controller {

    public function index() {

        $this->load->view('admin');
    }

    public function create_user() {

        $this->load->model('user_model');
        echo $this->user_model->create($_POST['email'], $_POST['password']);
    }

    public function delete_user($user_id) {

        $this->load->model('user_model');
        $this->user_model->delete($user_id);
    }
}
Admin

<form action="<?=site_url('admin/create_user')?>" method="post">
    Email: <input type="email" name="email" /><br />
    Password: <input type="password" name="password" /><br />
    <input type="submit" />
</form>
models/user_model.php

<?php
class User_model extends CI_Model {

    public function __construct() {

        parent::__construct();
    }

    public function create($email, $password) {

        return $this->db->insert('user', [
            'email' => $email,
            'password' => $password
        ]);
    }

    public function delete($user_id) {

        $this->db->where(['user_id' => $user_id]);
        return $this->db->delete('user');
    }
}
views/admin.php

<?php
class Admin extends CI_Controller {

    public function index() {

        $this->load->view('admin');
    }

    public function create_user() {

        $this->load->model('user_model');
        echo $this->user_model->create($_POST['email'], $_POST['password']);
    }

    public function delete_user($user_id) {

        $this->load->model('user_model');
        $this->user_model->delete($user_id);
    }
}
Admin

<form action="<?=site_url('admin/create_user')?>" method="post">
    Email: <input type="email" name="email" /><br />
    Password: <input type="password" name="password" /><br />
    <input type="submit" />
</form>

帮助…

因此,如果您看到空白页,请尝试在此处更改:

public function create($email, $password) {

    if ($this->db->insert('user', [
        'email' => $email,
        'password' => $password
    ])) {
      return 'User successfully inserted!';
    } else {
      return "Can't insert user: email=".$email."!";
    }
}

谢谢你帮助我!我想这是我过时的PHP版本。。。不支持[]而不使用数组 更新工作正常。
谢谢。

在您的型号中,插入部分有[]个括号,您不能这样做。此外,插入时不需要返回

在表单操作中,尝试用perferd base_url替换站点url

确保url和表单帮助程序已自动加载。请阅读用户指南

echo$this->user_model->create$_POST['email'],$_POST['password'];不好的尝试var转储帖子,也可以查看表单验证codeigniter。我认为这是因为您没有在controller中运行表单验证

代码点火器3在github BCIT上


帮什么忙??你有什么问题?以下哪些功能对您有好处?哪一个没有?所以当我输入详细信息并提交到表单中时,它返回一个空白页…您是否尝试过在controller的父构造中加载模型?@KimAlexander yea blank…no 404…@mustang83添加了它,但仍然返回空白页。。。lolI删除了类User_模型,并将其替换为一个简单的“echo test;”页面似乎正在加载,但我将数据传递到数据库的方法失败了…所以调用模型函数时没有错误消息?没有输出??怎么可能?试过测试我的模型是否有效。。。如果我删除了我的“create”和“delete”,并在“test”中用一个echo替换它,然后在URL中调用它,效果会很好…尝试注释parent::\u construct;在模型类中,为什么要管理/测试?您应该测试admin/create\u usernice。看起来干净多了!当您使用codeigniter时,您可以使用非常简单的方法来进行操作,例如,如果http://www.example.com/user/update/1 用户id应该是1,所以段$this->uri->segment3,您必须设置路由才能工作。