我正在关注CI用户指南中的https://www.codeigniter.com/user_guide/tutorial/create_news_items.html。
现在问题是:当我继续“index.php / news / create”时,数据没有插入数据库而没有成功重定向到success.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';
和 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');
}
}
模型/ 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);
}
}
,create.php和success.php文件与用户指南相同。有人指出错误是什么,
编辑:
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>
非常感谢提前! :)
答案 0 :(得分:1)
更改您的输入类型:
<input type="text" name="title" /><br />