dtutlamjasblef7982 2014-08-22 13:42
浏览 54
已采纳

Codeigniter指南创建新闻项目数据不插入

I am following https://www.codeigniter.com/user_guide/tutorial/create_news_items.html from CI User guide .

Now the problem is: when i proceed with "index.php/news/create", data not inserting into the database and not successfully redirecting to success.php

config/routes.php

    $route['default_controller'] = 'frontpage';
    $route['404_override'] = '';


    $route['(:any)'] = 'templates/view/$1';
    $route['(:any)'] = 'include/view/$1';


    $route['news/(:any)'] = 'news/$1';
    $route['news'] = 'news';
    $route['(:any)'] = 'pages/view/$1';

    $route['news/create'] = 'news/create';
    $route['news/(:any)'] = 'news/view/$1';

and the application/controller/news.php

<?php
class News extends CI_Controller {

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

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

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

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('text', 'text', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('include/header', $data);
        $this->load->view('news/create');
        $this->load->view('include/footer');

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

models/news_model.php

<?php
class News_model extends CI_Model {

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


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

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

    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'text' => $this->input->post('text')
    );

    return $this->db->insert('news', $data);
}

}

and the create.php and success.php files are same as user guide . can someone point what is the fault ,

Edited :

create.php

<h2>Create a news item</h2>

<?php echo validation_errors(); ?>

<?php echo form_open('index.php/news/success') ?>

    <label for="title">Title</label>
    <input type="input" name="title" /><br />

    <label for="text">Text</label>
    <textarea name="text"></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form>

Very much thanks in advance ! :)

  • 写回答

1条回答 默认 最新

  • drasv0904 2014-08-22 23:14
    关注

    Change your input type like this:

     <input type="text" name="title" /><br />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集