Php 无法使用codeigniter将值插入数据库

Php 无法使用codeigniter将值插入数据库,php,mysql,codeigniter,Php,Mysql,Codeigniter,尝试使用codeigniter将表单值插入数据库,但不使用heppens。 我的表单是comment\u form.php就像 <?php echo validation_errors(); ?> <?php echo form_open('news/comment_form'); ?> Name<input type="text" name="comment_name"></input><br /> Email<input t

尝试使用codeigniter将表单值插入数据库,但不使用heppens。 我的表单是comment\u form.php就像

<?php echo validation_errors(); ?>
<?php echo form_open('news/comment_form'); ?>

Name<input type="text" name="comment_name"></input><br />
Email<input type="text" name="comment_email"></input><br />
Comment<input type="text" name="comment_body"></input><br />
<input type="submit" name="submit" value="Comment it" ></input>

</form>
这是我的模型comment\u model.php

class Comments extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('comment_model');
    }

    public function create_comment()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');


        //$data['title'] = 'Create a news item';

        $this->form_validation->set_rules('comment_name', 'comment_name', 'required');
        $this->form_validation->set_rules('comment_email', 'comment_email', 'required');
        $this->form_validation->set_rules('comment_body', 'comment_body', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('news/comment_form');
            $this->load->view('templates/footer');
        } else {
            $this->news_model->set_comment();
            $this->load->view('news/success');
        }
    }
}
class Comment_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
    }


    public function set_comment()
    {
        //$this->load->helper('url');

        //$slug = url_title($this->input->post('title'), 'dash', TRUE);

        $datac = array(
            'comment_name' => $this->input->post('comment_name'),
            'comment_email' => $this->input->post('comment_email'),
            'comment_body' => $this->input->post('comment_body')
        );

        return $this->db->insert('comments', $datac);
    }
}

问题是,每当我提交表单时,它都不会返回任何内容,就像没有发生任何事情一样。请帮助。

$this->db->insert('comments',$datac)之后的
set_comment
函数中只需使用下面的命令回显查询,并尝试在数据库中手动运行相同的查询,您可能会了解问题

echo$this->db->last_query()

更改:

$this->load->database();
echo form_open('news/comment_form');
$this->news_model->set_comment();
致:

如果将数据库库加载到
autoload.php
中会更好。或者在控制器的构造函数中

this->news_model->set_comment()
应该是

this->comment_model->set_comment()

PS:很抱歉格式化了。移动版不读取新行。

在控制器中而不是模型中获取发布的值,然后将它们传递给模型

控制器功能

public function create_comment()
{
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->form_validation->set_rules('comment_name', 'comment_name', 'required');
    $this->form_validation->set_rules('comment_email', 'comment_email', 'required');
    $this->form_validation->set_rules('comment_body', 'comment_body', 'required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('templates/header', $data);
        $this->load->view('news/comment_form');
        $this->load->view('templates/footer');
    } else {
        $data['comment_name']   =   $this->input->post('comment_name'),
        $data['comment_email']  =   $this->input->post('comment_email'),
        $data['comment_body']   =   $this->input->post('comment_body')

        $this->news_model->set_comment();
        $this->load->view('news/success');
    }
}
模型函数

public function set_comment($data)
{
    //$this->load->helper('url');

    //$slug = url_title($this->input->post('title'), 'dash', TRUE);

    $this->db->insert('comments', $data);

    return $this->db->insert_id();
}
编辑

如果遵循这些步骤,上述代码将起作用

进入
application/config
中的
database.php
,提供数据库连接设置,如
hostname
username
password
数据库
名称。
然后转到
application/config
中的
autoload.php
,像这样自动加载数据库库

$autoload['libraries'] = array('database');
同时删除html表单中输入的结束标记。
在继续之前,尝试在控制器中进行这样的测试

$post = $this->input->post();
echo '<pre>';
print_r($post);
$post=$this->input->post();
回声';
印刷(邮政);

这将确保您在comment\u form.php

中收到帖子数据

echo form_open('comments/create_comment');
$this->comment_model->set_comment();

为什么?因为您为form_open()提供的第一个参数是action参数。它将打开一个表单标记,如
。新闻/创建注释不是您要调用的正确页面。您的控制器名为
comments
,这就是您放置
comments/create\u comment
的原因

在您的comments.php中更改

echo form_open('comments/create_comment');
$this->comment_model->set_comment();

为什么?只是简单地指向错误的模型。可能是复制粘贴错误

在您的注释中\u model.php删除

并将其加载到config.php(在库数组中,添加“数据库”)

为什么?我认为这是更合适的解决方案。你可能会经常使用你的数据库,为什么每次都要加载呢


如果您仍然遇到问题,那么我们需要更多信息。到底是什么不起作用。为我们做一些调试。调用
$this->news\u model->set\u comment()
write
var\u dump($this->db->last\u query())之后

输入没有结束标记。请尝试删除并再次提交。
$this->comment_model->set_comment();
$this->load->database();