gpt4 book ai didi

php - 尝试向 MySQL 插入数据时出错

转载 作者:行者123 更新时间:2023-11-29 05:47:52 25 4
gpt4 key购买 nike

我正在使用以下方法向数据库中添加数据:

public function updatePayment($data) {
$result = 0;
$payment = new Payment();

// Payment is 'before'
if (isset($data['payday'])) {
if ($data['payday'] == 'before') {
$payment->setBefore(1);
$payment->setAfter(0);
$payment->setBeforeAfter(0);
$result = 1;
}
elseif ($data['payday' == 'after']) {
$payment->setBefore(0);
$payment->setAfter(1);
$payment->setBeforeAfter(0);
$result = 1;
}
elseif ($data['payday'] == 'before_after') {
$payment->setBefore(0);
$payment->setAfter(0);
$payment->setBeforeAfter(1);
$result = 1;
}
}
if ($result == 1) {
$this->em->persist($payment);
$this->em->flush();
}
return $result;
}

但是我得到以下错误:

An exception occurred while executing 'INSERT INTO payment (work_id, task_id, before, after, before_after, milestone, min, max, hourly_rate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, 1, 0, 0, null, null, null, null]:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'before, after, before_after, milestone, min, max, hourly_rate) VALUES (NULL, NUL' at line 1

这是我的表格的样子:

enter image description here

我只想将值添加到列 beforeafterbefore_after。所以我不认为我也必须向其他列插入值,因为默认值应该是 NULL

我做错了什么?

最佳答案

beforeafterreserved words in MySQL .

您需要在查询中使用反引号对它们进行转义,例如:

INSERT INTO payment 
(work_id, task_id, `before`, `after`, before_after, milestone, min, max, hourly_rate)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)

所以在 symfony 中,是这样的:

<?php
/** @Column(name="`before`", type="integer") */
private $before;
/** @Column(name="`after`", type="integer") */
private $after;

更好的选择是简单地将列重命名为其他名称。

关于php - 尝试向 MySQL 插入数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57944409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com