CakePHP 3.0:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误

时间:2015-06-06 11:24:52

标签: mysql cakephp-3.0

我使用CakePHP 3.0中的Form将数据保存到数据库。

//add.ctp
<div>
    <?= $this->Form->create($deposit) ?>
    <fieldset>
        <legend><?= __('Add') ?></legend>
        <?php
            echo $this->Form->input('date');
            echo $this->Form->input('profile_id', ['options' => $profiles]);
            echo $this->Form->input('amnt');
            echo $this->Form->input('desc');
            echo $this->Form->input('user_id', ['options' => $users]);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

这是我的添加功能

public function add()
{
    $deposit = $this->Deposits->newEntity();
    if ($this->request->is('post')) {
        $deposit = $this->Deposits->patchEntity($deposit, $this->request->data);
        if ($this->Deposits->save($deposit)) {
            $this->Flash->success(__('The member deposit has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The member deposit could not be saved. Please, try again.'));
        }
    }

    $profiles = $this->Deposits->Profiles->find('list', ['limit' => 200]);
    $users = $this->Deposits->Users->find('list', ['limit' => 200]);
    $this->set(compact('deposit', 'profiles', 'users'));
}

当我提交下面找到的表格时,我发现数据库syntex错误

  

错误:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在'desc,user_id,created,modified)附近使用正确的语法VALUES('2015-06-06',7,'3211','some text',1,'在第1行

SQL Query显示:

  

INSERT INTO member_deposits(date,profile_id,amnt,desc,user_id,created,modified)VALUES(:c0,:c1,:c2,:c3,:c4,:c5,:c6)

我花了很多时间通过谷歌搜索解决问题,并从Similar Post,没有运气,但花了一天后,我发现只需将 quoteIdentifiers 配置为 true < /强>

默认情况下,

quoteIdentifiers在您的蛋糕项目的数据源下的config / app.php中设置为false。

1 个答案:

答案 0 :(得分:3)

你的一个专栏是使用MySQL保留的列名。

dev.mysql.com/doc/refman/5.0/en/reserved-words.html

如您所见,DESC是保留的。

如果你能找到一种方法来改变查询以使用转义列名指定,mysql将容忍这一点。例如

INSERT INTO `member_deposits` (
    `date`, `profile_id`, `amnt`,
    `desc`, `user_id`, `created`, `modified`) 
VALUES (:c0, :c1, :c2, :c3, :c4, :c5, :c6)

或者,将列名更改为不违反mysql保留字规则的内容。