我是cake php的新手。我想将表单数据插入数据库。但无法插入。请帮助
控制器:
officetypesController.php文件
<?php
class OfficetypesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->Officetype->create();
if ($this->Officetype->save($this->request->data)) {
$this->Session->setFlash(__('The data has been saved'));
}
$this->Session->setFlash(
__('The data could not be saved. Please, try again.')
);
}
}
}
?>
看法
附加ctp
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php echo $this->Form->input('name');
//echo $this->Form->input('password');
echo $this->Form->input('under', array(
'options' => array('1' => 'HO', '2' => 'RO')
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
模型
officetype.php文件
<?php
class Officetype extends AppModel {
}
数据库:
表名:officetypes,
字段名:id,name,under
当我点击提交按钮时,显示消息“数据无法保存”。请再试一次。“
最佳答案
public function add() {
if ($this->request->is('post')) {
$this->Officetype->create();
if ($this->Officetype->save($this->request->data)) {
$this->Session->setFlash(__('The data has been saved'));
} else {
$this->Session->setFlash(__('The data could not be saved. Please, try again.'));
}
}
}
检查数据库以查看是否添加了记录。
如果要添加office类型,为什么表单与用户相关?
<div class="office_type form">
<?php echo $this->Form->create('Officetype'); ?>
<fieldset>
<legend><?php echo __('Add Office type'); ?></legend>
<?php echo $this->Form->input('name');
echo $this->Form->input('under', array(
'options' => array('1' => 'HO', '2' => 'RO')
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
关于php - cakephp将数据保存到数据库中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21349066/