gpt4 book ai didi

php - CakePHP 数据未保存/添加到数据库

转载 作者:可可西里 更新时间:2023-11-01 01:04:10 25 4
gpt4 key购买 nike

提交数据只会重新加载页面,CakePHP 不会给出任何错误或消息。代码遵循与博客教程相同/相似的结构。

查看代码

        <?php
echo $this->Form->create('Sm');
echo $this->Form->input('recievers', array('rows' => '1'));
echo $this->Form->input('subject');
echo $this->Form->input('message');
echo $this->Form->end('SEND');
?>

Controller 代码

    public function send() {
if ($this->request->is('sm')) {
$this->Sm->create();
if ($this->Sm->save($this->request->data)) {
$this->Session->setFlash('Sms has been added to the database');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to send sms.');
}
}
}

模型代码

class Sm extends AppModel {
public $validate = array(
'subject' => array(
'rule' => 'notEmpty'
),
'message' => array(
'rule' => 'notEmpty'
),
'recievers' => array(
'rule' => 'notEmpty'
)
); }

导出的 SQL

    CREATE TABLE IF NOT EXISTS `sms` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`subject` varchar(150) DEFAULT NULL,
`message` text,
`sender` varchar(50) DEFAULT NULL,
`recievers` text,
`sent` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

最佳答案

您指定了错误的请求“方法”;

if ($this->request->is('sm')) {

应该是:

if ($this->request->is('post')) {

我怀疑您对 CakePHP 上的示例感到困惑,这些示例使用了“post”模型。然而,这一行是为了检查用于访问页面的请求类型,例如postgetput

通过检查正确的请求方法,CakePHP 只会在发送/提交表单时插入/更新数据,否则只显示表单而不更新数据库

关于php - CakePHP 数据未保存/添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15201262/

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