我是cakephp的新手,我想在数据库中插入表单数据,但不插入值。我哪里错了?这是我的观点(首先我只想添加一个字段)。提前感谢
视图-
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->end('Add');
在控制器中=
public function index()
{
$this->layout=false;
if($this->is->request('post'):
$this->request->data["Post"]["title"]=$this->request->data["Post"]["title"]
$this->Post->save($this->request->data);
endif;
}
您需要检查控制器代码。
试试这个:
public function index() {
$this->layout=false;
if($this->request->is('post')) {
if ($this->Post->save($this->request->data)) {
echo "Save successful!";
} else {
echo "Save failed!";
}
exit;
}
}
希望这能有所帮助。
这里我可以给你一个例子,用多个字段插入cakepp中的任何记录
只要看看功能,你可能会对有个好主意
$data = $this->request->data;
if (!empty($data)) {
$ret_save = $this->Post->save();
if ($ret_save) {
$this->Session->setFlash(__('Save successful!'));
$this->redirect(array('controller' => 'controller_name', 'action' => 'index'));
}else{
echo "Save failed!";
}
希望这段代码能对你有所帮助。