dongqian9567 2014-03-24 09:42
浏览 15

当'post'替换为'get'时,为什么CakePHP会添加新行?

I edit a row on the Post table or add a new row to it from edit() function in PostsController. The function looks like this:

public function edit($id = null) {
// Has any form data been POSTed?
if ($this->request->is('post')) { //Replaced 'post' by 'get' in this line

    // If the form data can be validated and saved...
    if ($this->Post->save($this->request->data)) {

        // Set a session flash message and redirect.
        $this->Session->setFlash('Updated the Post!');
        return $this->redirect('/posts');
    }
}

// If no form data, find the post to be edited
// and hand it to the view.
$this->set('post', $this->Post->findById($id));

}

I simply replaced 'post' by 'get' to see what would happen and it went on creating new rows without even taking me to the form. I still get the flash message 'Updated the Post!', but without taking any form data.

If the code in edit.ctp is required, here it is:

<?php
echo $this->Form->Create('Post');
echo $this->Form->input('id', array('type' => 'hidden','default'=>$post['Post' ['id']));
    echo $this->Form->input('title',array('default'=>$post['Post']['title']));
    echo $this->Form->input('body',array('default'=>$post['Post']['body']));
    echo $this->Form->end('Update');
    ?>

Any thoughts on why this might be happening?

Edit: Added CakePHP Version

I am using CakePHP 2.4.5

  • 写回答

1条回答 默认 最新

  • douliang1900 2014-03-24 10:52
    关注

    What you are doing makes no sense. Why would you want to switch the "post" by "get" here? Of course it will then generate new rows, as you effectively trigger a save on each page load (GET). Don't do that. The code you had there was just fine - IF you also took PUT into consideration. For edit forms, it is not a post, but:

    if ($this->request->is('put')) {}
    

    PS: If you want to make sure it always works for both add/edit, use

     if ($this->request->is(array('post', 'put')) {}
    

    But NEVER replace it with "get".

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题