我目前正在尝试使用 codeigniter 将数据添加到数据库。我已经使用事件方法设置了一个注册页面,并尝试使用相同的方法添加新闻表单,但没有成功。
当我点击提交时,它说找不到页面,并且 url 显示 Controller 函数名称。当我故意将任何字段留空时也是如此。我检查了我的数据库,没有添加记录,也没有 php 日志错误。
这是我的代码片段:
查看:
<?php echo form_open('add/add_article'); ?>
<?php echo form_input('title', set_value('title', 'Title')); ?><br />
<?php echo form_textarea('content', set_value('content', 'Content')); ?><br />
<?php echo form_input('author', set_value('author', 'Author')); ?>
<?php echo form_submit('submit', 'Add Article'); ?>
<?php echo validation_errors('<p class="error">' );?>
<?php echo form_close(); ?>
Controller :
class Add extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('admin/add');
}
public function add_article() {
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('content', 'Content', 'trim|required');
$this->form_validation->set_rules('author', 'Author', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->index();
}else{
$this->load->model('news_model');
if($query = $this->news_model->addArticle()) {
$this->load->view('news');
}else {
$this->load->view('news');
}
}
}
}
型号:
public function __construct() {
parent::__construct();
}
function addArticle() {
$data =array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'author' => $this->input->post('author'),
'username' => $this->input->post('username'));
$insert = $this->db->insert('news', $data);
return $insert;
}
}
最佳答案
如果是服务器抛出找不到页面的错误,则几乎可以肯定是 URL 问题,而不是 CI/PHP 问题。
您的基本网址在配置文件中定义正确吗?您的 .htaccess
配置是否正确(旧配置可能会从 CI 路由/添加请求)?
尝试将以下操作添加到“添加” Controller ,并直接在 http://[base]/add/thetest
导航到该 Controller
public function thetest() {
echo 'Controller accessed';
die;
}
如果它仍然显示未找到页面,那么这不是您的代码,而是您的配置(服务器配置或 CI)。
关于php - 使用codeigniter向数据库添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12267476/