尝试更新数值时出现语法错误 (MySQL)
Getting syntax error when trying to update numerical value (MySQL)
我是一名业余程序员,创建了一个基于 PHP 的在线门户,它将更新 MySQL 数据库中与 MMO 类型游戏相关的值,我们使用该门户来跟踪每个用户保护的地块总数。
我正在编写脚本,该脚本将在通过 $_POST 数组提交 HTML 表格后更新给定类型保护区的 table 计数。
有问题的 MySQL table (players
) 有四个相似的字段(以及其他字段):
- wild_count
- city_count
- nether_count
- end_count
在 HTML 表单中,用户可以在提交时 select 土地类型,脚本会尝试执行字符串连接以完成该字段,然后将其提供给占位符准备好 SQL 查询,例如:
//Set land type string
$landtype = $_POST['landtype'] . '_count';
//Process ADD request
if (!isset($_POST['negative']))
{
$action = 'ADDED'; //This is for a transaction report further down in the code
try
{
$sql = 'UPDATE players SET
`:landtype` = `:landtype` + :tiles WHERE id = :id';
$query = $link->prepare($sql);
$query->bindValue(':landtype', $landtype);
$query->bindValue(':tiles', $_POST['tiles']);
$query->bindValue(':id', $_POST['player']);
$query->execute();
}
catch (PDOException $e)
{
$error = 'Error updating land count: ' . $e->getMessage();
include './includes/error.inc.php';
exit();
}
...more code follows...
尝试使用以下代码 POST 我的表单时,出现以下错误:
Error updating land count: SQLSTATE[42S22]: Column not found: 1054 Unknown column ''city_count'' in 'field list'
(我在表单示例中 selected city
)。
我尝试了相同的代码,除了占位符 :landtype
周围没有反引号(即 $sql = 'UPDATE players SET :landtype = :landtype + :tiles WHERE id = :id';
),我得到了一个不同的错误:
Error updating land count: 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 ''city_count' = 'city_count' + '300' WHERE id = '1'' at line 2
我不确定如何进行。通过创建连接字符串来设置字段值的尝试是否会在此处中断?
不要尝试像绑定值一样绑定列名:
$sql = 'UPDATE players SET `'.$landtype.'` = `'.$landtype.'` + :tiles WHERE id = :id';
Can PHP PDO Statements accept the table or column name as parameter?
我是一名业余程序员,创建了一个基于 PHP 的在线门户,它将更新 MySQL 数据库中与 MMO 类型游戏相关的值,我们使用该门户来跟踪每个用户保护的地块总数。
我正在编写脚本,该脚本将在通过 $_POST 数组提交 HTML 表格后更新给定类型保护区的 table 计数。
有问题的 MySQL table (players
) 有四个相似的字段(以及其他字段):
- wild_count
- city_count
- nether_count
- end_count
在 HTML 表单中,用户可以在提交时 select 土地类型,脚本会尝试执行字符串连接以完成该字段,然后将其提供给占位符准备好 SQL 查询,例如:
//Set land type string
$landtype = $_POST['landtype'] . '_count';
//Process ADD request
if (!isset($_POST['negative']))
{
$action = 'ADDED'; //This is for a transaction report further down in the code
try
{
$sql = 'UPDATE players SET
`:landtype` = `:landtype` + :tiles WHERE id = :id';
$query = $link->prepare($sql);
$query->bindValue(':landtype', $landtype);
$query->bindValue(':tiles', $_POST['tiles']);
$query->bindValue(':id', $_POST['player']);
$query->execute();
}
catch (PDOException $e)
{
$error = 'Error updating land count: ' . $e->getMessage();
include './includes/error.inc.php';
exit();
}
...more code follows...
尝试使用以下代码 POST 我的表单时,出现以下错误:
Error updating land count: SQLSTATE[42S22]: Column not found: 1054 Unknown column ''city_count'' in 'field list'
(我在表单示例中 selected city
)。
我尝试了相同的代码,除了占位符 :landtype
周围没有反引号(即 $sql = 'UPDATE players SET :landtype = :landtype + :tiles WHERE id = :id';
),我得到了一个不同的错误:
Error updating land count: 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 ''city_count' = 'city_count' + '300' WHERE id = '1'' at line 2
我不确定如何进行。通过创建连接字符串来设置字段值的尝试是否会在此处中断?
不要尝试像绑定值一样绑定列名:
$sql = 'UPDATE players SET `'.$landtype.'` = `'.$landtype.'` + :tiles WHERE id = :id';
Can PHP PDO Statements accept the table or column name as parameter?