【问题标题】:Fatal error: Call to undefined function validation_errors() using codeIgniter致命错误:使用 codeIgniter 调用未定义的函数 validation_errors()
【发布时间】:2013-10-28 14:28:47
【问题描述】:

致命错误:使用 codeIgniter 调用未定义的函数 validation_errors() 这是我的 cmets.php 视图

        <?php echo validation_errors(); ?>

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

            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>

这是我的 news_model.php

<?php
class News_model extends CI_Model {

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



//set comment

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

$data_c = 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', $data_c);
}

}

这里是我的控制器news.php

<?php
class News extends CI_Controller {

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


}



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)
{
    echo 'failed'; //just for debugging

}
else
{
    $this->news_model->set_comment();
    $this->load->view('news/success');
}   


}

}

routes.php

$route['news/comments'] = 'news/comments';

如何解决?由于致命错误,无法将数据插入数据库。

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    我是整个编程领域的新手。 我想 HTML 输入字段应该有标签 ID。所以模型中的数组可以读取它们。

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

    【讨论】:

    • PHP 无法通过 ID 读取表单输入,只能读取名称属性。你也不要关闭input类型标签。
    【解决方案2】:

    我没有看到您在任何地方加载您的 validation_form 库 - 您确定它已加载吗?

    要么在自动加载中,要么:

    $this->load->library('form_validation');
    

    【讨论】:

    • 我已经加载了自动加载并且它工作了......现在的问题是当我提交值时它没有被存储到数据库中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 2019-11-10
    相关资源
    最近更新 更多