CakePHP 3.0:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有误
CakePHP 3.0: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax
我在 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'));
}
当我提交表单时发现数据库语法错误
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)
我花了很多时间通过谷歌搜索和 Similar Post 来解决这个问题,运气不好,但在花了一天时间后我发现只需将 quoteIdentifiers 配置为真.
quoteIdentifiers 在蛋糕项目的 Datasources 下的 config/app.php 默认设置为 false。
您的一个列正在使用 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 保留字规则的名称。
我在 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'));
}
当我提交表单时发现数据库语法错误
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)
我花了很多时间通过谷歌搜索和 Similar Post 来解决这个问题,运气不好,但在花了一天时间后我发现只需将 quoteIdentifiers 配置为真.
quoteIdentifiers 在蛋糕项目的 Datasources 下的 config/app.php 默认设置为 false。
您的一个列正在使用 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 保留字规则的名称。