gpt4 book ai didi

php - cakephp 保存数据到数据库

转载 作者:行者123 更新时间:2023-11-29 06:41:24 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com