2013-10-22 76 views
0

尝试使用codeigniter将表单值插入到数据库中,但没有发生任何事件。 我的形式是comment_form.php是什么样子,无法使用codeigniter将值插入到数据库中

<?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> 

这里是我的控制器的comments.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'); 
     } 
    } 
} 

,这是我的模型comment_model.php

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); 
    } 
} 

问题是每当我苏它不会返回任何形式,就像没有任何事情发生一样。请帮助。

+0

输入没有结束标记。尝试删除并再次提交。 –

回答

0

在你comment_form.php变化

echo form_open('news/create_comment'); 

echo form_open('comments/create_comment'); 

为什么?因为你给form_open()的第一个参数是动作参数。它会打开一个表格标签,如<form action="bla">。而news/create_comment不是您想要调用的正确页面。您的控制器名为comments,这就是为什么你把comments/create_comment

在你的comments.php变化

$this->news_model->set_comment(); 

$this->comment_model->set_comment(); 

为什么?只是简单地指向假模型。也许是一个复制错误?

在你comment_model.php删除

$this->load->database(); 

而在你的config.php加载(库阵列中,添加 '数据库')。

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

如果您仍然遇到问题,那么我们需要更多信息。什么是不工作。为我们做一些调试。致电$this->news_model->set_comment()后写var_dump($this->db->last_query());

+0

感谢帕特里克,非常感谢。 –

+0

很高兴我能帮忙:) –

0

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

echo $ this-> db-> last_query();

+0

不能正常工作。 –

0

变化:

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

要:

$this->load->library('database'); 
echo form_open('news/create_comment'); 
$this->comment_model->set_comment(); 

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

+0

它对您有用吗? –

+0

对不起Nil'z它不起作用, –

0
this->news_model->set_comment() 

应该

this->comment_model->set_comment() 

PS:遗憾的格式。手机版不读新行。

+0

感谢纠正,但仍存在问题。 –

0

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

控制器功能

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(); 
} 

EDITS

,如果您按照这些步骤将上面的代码工作。

转到database.phpapplication/config,并提供像hostnameusernamepassworddatabase名称数据库连接设置。
然后去autoload.phpapplication/config和自动加载数据库库这样

$autoload['libraries'] = array('database'); 

而且删除HTML表单输入的结束标记。在控制器
尝试测试这样的前进

$post = $this->input->post(); 
echo '<pre>'; 
print_r($post); 

之前,这将确保您收到后数据

+0

尝试过但不工作,存在同样的问题。 –

+0

@AshishSingh查看编辑 –

相关问题