2015-06-06 53 views
1

我在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下的數據源你的蛋糕項目。

+2

我不知道cakePHP,但我懷疑這是您對列'DESC'的保留字的不幸使用 - https://dev.mysql.com/doc/refman/5.0/zh/reserved-words html的。 要麼找到一種方法來逃避字段名稱(例如'desc')或更改列名稱。 – crafter

+0

這裏有問題嗎? – ndm

+0

@crafter,如果我將列** desc **更改爲任何內容,它都可以工作。謝謝。 –

回答

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保留字規則的內容。

+0

謝謝回答.. –

相關問題