数据未保存在数据库中

时间:2016-04-28 09:00:13

标签: php cakephp

我在CakePHP工作。我想将发布的数据保存到我的数据库中。所以我在控制器中创建了如下所示的函数:

function add_news()
{
    $this->_checkSession();
    $this->layout = "admin_inner";

    if(!empty($this->params['form'])) 
    {   
        $tit = $this->params['form']['title'];
        $con = $this->params['form']['content'];
        $query = "insert into news(news_title,news_content) values('".$tit."','".$con."')";
        echo $query;
        $this->News->query($query);
    }

    Configure::write('debug', '2');
}

我打印查询并在数据库中执行然后它工作正常。但这里它没有执行。我没有新闻模型。

注意:我在CakePHP 1.3.13

工作

2 个答案:

答案 0 :(得分:0)

首先你需要知道如何在cakephp中插入数据....链接是这样的: - http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html

然后您可以尝试使用此代码在表

中插入数据
if(!empty($this->params['form'])) 
{   
    $tit = $this->params['form']['title'];
    $con = $this->params['form']['content'];

    $data = array(
            "news_title"=>$tit,
            "news_content"=>$con

            );
    $this->News->save($data);       

}

答案 1 :(得分:0)

function add_news()
{
    $this->_checkSession();
    $this->layout = "admin_inner";

    if(!empty($this->params['form'])) 
    {   
        $tit = $this->params['form']['title'];
        $con = $this->params['form']['content'];
        $data = array(
            "news_title"=>$tit,
            "news_content"=>$con

           );
     $this->News->save($data); 
   }

   Configure::write('debug', '2');

}