繁体   English   中英

cakephp将数据保存到数据库中

[英]cakephp saving data into database

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM