【发布时间】:2014-10-16 09:26:35
【问题描述】:
我正在关注 CI 用户指南中的 https://www.codeigniter.com/user_guide/tutorial/create_news_items.html。
现在的问题是:当我继续“index.php/news/create”时,数据没有插入数据库并且没有成功重定向到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';
和 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);
}
}
create.php 和 success.php 文件与用户指南相同。谁能指出是什么问题,
已编辑:
创建.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>
非常感谢提前! :)
【问题讨论】:
-
是否有任何mysql错误显示或者它真的在模型内部传递?
-
嗨@reignsly:当我点击“创建新闻项目按钮”时,它重定向到“localhost/news/create”它应该是“localhost/index.php/news/success”,我认为对吗?
-
在你的表单中,它说它将发布到
localhost/news/create,从这里它将使用模型保存该数据。然后当成功时它会重定向到news/success。但是如果数据保存不成功,它将停留在/new/create。如果返回 true,我建议您检查保存和验证表单。 :) -
嗨@reignsly,当我运行“/index.php/news/create”路径时,它显示“string(0)”“失败”
标签: php codeigniter phpmyadmin routes