php - cakephp 保存数据到数据库

标签 php mysql cakephp

我是 cake php 的新手。我想将表单数据插入数据库。但无法插入。请帮忙

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

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

为什么要增加办公类型,表单与用户相关?

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

相关文章:

cakephp - 如何从 select-multiple 类型的表单输入中删除包装 div

php - PDO::FETCH_ASSOC 没有获取所有内容

php - 遍历文本文件并检查文件是否存在于服务器上

MySQL:无法选择具有时间范围的数据

mysql - 无法找到逻辑名为 : propertymasterfk in org. hibernate.mapping.Table(mastertable) 的列及其相关的超表和辅助表

PHP - MySql 数组

javascript - 我怎样才能停止 AJAX 调用保持 PHP session 事件

通过 SSL 的 php SOAP 调用 - 如何在请求中同时插入证书、 key 和 CA 证书?

php - 使用条件 CSS 解决错误的 JavaScript div 位置更改

View 和 $this 中的 Cakephp 助手