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

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 ;

图片转代码服务由CSDN问答提供 功能建议

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

视图代码

 &lt;?php 
 echo $ this-&gt; Form-&gt  ; create('Sm'); 
 echo $ this-&gt; Form-&gt; input('recievers',array('rows'=&gt;'1')); 
 echo $ this-&gt; Form-  &gt;输入('subject'); 
 echo $ this-&gt; Form-&gt; input('message'); 
 echo $ this-&gt; Form-&gt; end('SEND'); 
?  &gt; 
   
 
 

控制器代码

  public function send(){
 if($ this-&gt;  request-&gt;是('sm')){
 $ this-&gt; Sm-&gt; create(); 
 if($ this-&gt; sm-&gt; save($ this-&gt; request-&gt;  ; data)){
 $ this-&gt; Session-&gt; setFlash('sms已添加到数据库'); 
 $ this-&gt; redirect(array('action'=&gt;'index')  ); 
} else {
 $ this-&gt; Session-&gt; setFlash('unable to s 结束短信。'); 
} 
} 
} 
   
 
 

模型代码

  class Sm 扩展AppModel {
public $ validate = array(
'subject'=&gt; 数组(
'规则'=&gt;'notEmpty'
),
'消息'=&gt; 数组(
'规则'=&gt;'notEmpty'
),
'recievers'=&gt; 数组(
'规则'=&gt;'notEmpty'
)
);  } 
   
 
 

导出的SQL

  CREATE TABLE IF NOT NOT EXISTS`ms`(
`id`int(  10)unsigned NOT NULL AUTO_INCREMENT,
`the subject`varchar(150)DEFAULT NULL,
`message`文本,
`insentter`varchar(50)DEFAULT NULL,
`recievers`文本,
`sesent`  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

    点赞 评论
  • dsc862009 2013-03-04 12:15

    You forget to define name property inside model.

    var $name = 'Sm';
    
    点赞 评论
  • doufei5537 2015-01-27 10:10

    Just a few minutes ago, I was facing same type of problem. My problem was more weird. In my controller I used something like this:

    if( $this->MyModel->save( $this->request->data ) ) {
        //positive. proceed.....
    }
    else {
        //negative. show error
        pr( $this->MyModel->validationErrors );
    }
    

    As you can see I've handled the negative case but still I could see nothing. Even in my model, I used beforeSave and afterSave to check. In my beforeSave model, I could see a perfectly formatted array ready to be saved but I afterSave was not triggering which means the data was not saved. Hence I should see error from my controller. Still no solution.

    Now this is how I figured out the problem. I checked the table the data is gonna be saved into. The table had lots of columns with NOT NULL attribute. The data I was saving had some fields with NULL values. So theoretically, I should see validationErrors in the controller as a reason but unfortunately it was not showing. Setting those fields nullable solved my problem. So my suggestion to you is to check which fields might have NULL value and set those nullable and make sure the NOT NULL fields have some values.

    Hope this helps. Cheers!!!

    点赞 评论

相关推荐 更多相似问题