我在CakePHP 3.0中使用Form將數據保存到數據庫。CakePHP 3.0:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法中有錯誤
//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錯誤
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, user_id, created, modified) VALUES ('2015-06-06', 7, '3211', 'some text', 1,' at line 1
和SQL查詢的形式顯示:
INSERT INTO member_deposits (date, profile_id, amnt, desc, user_id, created, modified) VALUES (:c0, :c1, :c2, :c3, :c4, :c5, :c6)
我花了很多時間resolvin g谷歌搜索的問題和從Similar Post,沒有運氣,但花了一天後,我發現得到解決簡單配置quoteIdentifiers至真。
quoteIdentifiers默認設置爲false,在config/app.php下的數據源你的蛋糕項目。
我不知道cakePHP,但我懷疑這是您對列'DESC'的保留字的不幸使用 - https://dev.mysql.com/doc/refman/5.0/zh/reserved-words html的。 要麼找到一種方法來逃避字段名稱(例如'desc')或更改列名稱。 – crafter
這裏有問題嗎? – ndm
@crafter,如果我將列** desc **更改爲任何內容,它都可以工作。謝謝。 –