dongqiu7365 2013-03-04 12:09
浏览 82
已采纳

CakePHP数据不保存/添加到数据库

Submitting the data only reloads the page, no errors or messages is given by CakePHP. The code follows the same/similar structure as the blog tutorial.

The view code

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

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

Model code

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

Exported 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 ;
  • 写回答

3条回答 默认 最新

  • duanchuaiwan0063 2013-03-04 14:07
    关注

    You've specified an incorrect request 'method';

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

    Should be:

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

    I suspect you got confused by the examples on CakePHP, which use a 'post' Model. However this line is to check the type of request used to access the page, e.g. post, get, put.

    By checking for the right request method, CakePHP will only insert/update the data is the form is sent/submitted, otherwise the form is just shown without updating the database

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题