Php 使用codeigniter向数据库添加数据
我目前正在尝试使用codeigniter向数据库添加数据。我已经使用活动方法设置了一个注册页面,并尝试对添加新闻表单使用相同的方法,但未成功 当我单击submit时,它表示找不到页面,url显示控制器函数名。这与我故意将任何字段留空时是一样的。我已经检查了我的数据库,没有添加任何记录,也没有php日志错误 以下是我的代码片段: 视图: 型号:Php 使用codeigniter向数据库添加数据,php,database,codeigniter,content-management-system,add,Php,Database,Codeigniter,Content Management System,Add,我目前正在尝试使用codeigniter向数据库添加数据。我已经使用活动方法设置了一个注册页面,并尝试对添加新闻表单使用相同的方法,但未成功 当我单击submit时,它表示找不到页面,url显示控制器函数名。这与我故意将任何字段留空时是一样的。我已经检查了我的数据库,没有添加任何记录,也没有php日志错误 以下是我的代码片段: 视图: 型号: public function __construct() { parent::__construct(); } functio
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;
}
}
在模型中使用更新,而不是插入,如:
$insert = $this->db->update('news', $data);
return $insert;
我认为控制器中的这部分代码也是错误的(错误的if语句,并且没有数据发送到模型):
试试这个:
$data =array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'author' => $this->input->post('author'),
'username' => $this->input->post('username')
);
$query = $this->news_model->addArticle($data);
if($query)
{
// query ok
$this->load->view('news');
}
else {
// no query
$this->load->view('news');
}
如果是服务器抛出未找到的页面,那么几乎可以肯定是URL问题,而不是CI/PHP问题 配置文件中是否正确定义了基本url?您的
.htaccess
配置是否正确(旧配置可能是从CI路由/添加请求)
尝试将以下操作添加到Add控制器,并直接在http://[base]/Add/thetest中导航到它
public function thetest() {
echo 'Controller accessed';
die;
}
如果它仍然说找不到页面,那不是您的代码,而是您的配置(服务器配置或CI).你在source中看到表单的操作是什么?在source中,我知道该操作是针对add controller和add_article函数的,提交时只显示:localhost/codeigniter/add/add_controller和page not found,这是你的意思吗?我还尝试在if语句的控制器中使用重定向代码>---如果你想看的话,我已经在我的第一篇文章下面添加了我的工作代码。--我已经保存了这个代码片段以备将来参考,但是当使用$insert=$this->db->update('news',$data)时;这更新了数据库中的所有记录,而不是添加新行。我尝试了这一操作,但在测试时仍然抛出了一个未找到的CI页面url。我在添加/测试中添加了admin,并找到了echo,因此现在我已将“admin”添加到打开的表单中,如果在我的控制器中进行查询。非常感谢。
if($query = $this->news_model->addArticle()) {
$this->load->view('news');
}else {
$this->load->view('news');
}
$data =array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'author' => $this->input->post('author'),
'username' => $this->input->post('username')
);
$query = $this->news_model->addArticle($data);
if($query)
{
// query ok
$this->load->view('news');
}
else {
// no query
$this->load->view('news');
}
public function thetest() {
echo 'Controller accessed';
die;
}