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

Bay*_*lam 1 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>
Run Code Online (Sandbox Code Playgroud)

这是我的添加功能

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'));
}
Run Code Online (Sandbox Code Playgroud)

当我提交我在数据库语法错误下面找到的表格时

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

SQL 查询显示:

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

我花了很多时间通过谷歌搜索和类似帖子解决这个问题,没有运气,但花了一天后,我发现只需将quoteIdentifiers配置为true即可解决。

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

cra*_*ter 5

您的一列正在使用 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)
Run Code Online (Sandbox Code Playgroud)

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