cakephp将数据保存到数据库中

时间:2014-01-25 10:00:17

标签: php mysql cakephp

我是蛋糕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.')
            );
        }
    }
}
?>

视图

add.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,在

当我点击提交按钮而不是显示消息“数据无法保存。请再试一次。”

1 个答案:

答案 0 :(得分:2)

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.'));
        }
    }
}

检查数据库以查看是否添加了记录。

如果您想添加办公室类型,为什么表单与用户相关?

<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>