php - 我如何删除此错误 : PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

标签 php mysql pdo

我有这个错误:PDOException: SQLSTATE[HY093]: 参数号无效:参数未定义。

这是代码:

$query = "INSERT INTO ucp_beschwerde (date, tat_name, opfer_name, vergehen, content, one-url, two-url, three-url) VALUES (:date, :tat_name, :opfer_name, :vergehen, :content, :one-url, :two-url, :three-url)";
$smt = $db->prepare($query);
$smt->execute(array(':date' => $date, ':tat_name' => $_POST['beschuldigter'], ':opfer_name' => $aaa['Name'], ':vergehen' => $_POST['vergehen'], ':content' => $_POST['content'], ':one-url' => $_POST['one-bild'], ':two-url' => $_POST['two-bild'], ':three-url' => $_POST['three-bild']));

最佳答案

包含连字符的表格和列必须用反引号括起来。

SQL 认为你想做数学运算,它被悄悄地解释为“one minus url”等等

因此,您可以将代码更改为以下内容:

(date, tat_name, opfer_name, vergehen, content, `one-url`, `two-url`, `three-url`)

或通过将连字符更改为下划线 _ 来重命名您的列。

(date, tat_name, opfer_name, vergehen, content, one_url, two_url, three_url)

有关标识符的更多信息,请访问:

引用手册:

If any components of a multiple-part name require quoting,  
quote them individually rather than quoting the name as a whole.  
For example, write `my-table`.`my-column`, not `my-table.my-column`.

参见 Bill Karwin's answer还有。

摘自 Bill 的回答:

是的,如果您使用定界标识符,您可以使用标点符号、空格、国际字符和 SQL 保留字:

SELECT * FROM `my-table`;

在 MySQL 中,使用反引号。在标准 SQL 中,使用双引号。

或者如果你使用 MySQL,你可以设置 ANSI_QUOTES SQL mode :

SET SQL_MODE = ANSI_QUOTES;
SELECT * FROM "my-table";

在连接打开后立即添加 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);,如果您尚未这样做的话。

关于php - 我如何删除此错误 : PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27342956/

相关文章:

php - 如何实现 gmail 登录?

php - 查询以检查 MYSQL 表上是否存在触发器

javascript - AngularJS 联系表

php 版本的 mysql str_to_date()

php - Kubuntu 16.04 - PHP 5.6 - mssql 驱动程序

php - 是否可以在 PDOStatement 对象的单个实例上执行多个 fetch() 操作?

php - 如何像 prompt() 但在 PHP 中获取用户输入?

mysql - 如何使用子查询更新特定列多行

python - SQLAlchemy:查询期间失去与 MySQL 服务器的连接

php - 如何从多次包含该值的表中仅获取 1 个结果?