简体   繁体   English

绑定参数错误:致命错误:未捕获的 PDOException:SQLSTATE[HY093]:无效的参数号:未定义参数

[英]Binding parameters error:Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in

I have a very simple form with the php code to insert there data into a database.我有一个非常简单的表格,带有 php 代码,可以将数据插入数据库。 If I run it without binding the parameters, it works perfectly, but as soon as I bund them, I get an error: Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in.如果我在不绑定参数的情况下运行它,它可以完美运行,但是一旦绑定它们,我就会收到错误:致命错误:未捕获的 PDOException:SQLSTATE [HY093]:无效的参数编号:未定义参数。

my code is as follows:我的代码如下:

<?php
include('../../functions/database.php');

$conn = create_connection();

$id = $_POST['id'];
$intId = intval($id);
$name = $_POST['name'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$address_town = $_POST['address-town'];
$address_country = $_POST['address-country'];

$stm = "INSERT INTO company_address (name, company_id, address_1, address_2, address_3, address_town, address_country) values(:name, $intId, :address1, :address2, :address3, :address-town, :address-country)";

$query = $conn->prepare($stm);
$query->bindValue(':name',htmlspecialchars($name, ENT_QUOTES));
$query->bindValue(':address1',htmlspecialchars($address1, ENT_QUOTES));
$query->bindValue(':address2',htmlspecialchars($address2, ENT_QUOTES));
$query->bindValue(':address3',htmlspecialchars($address3, ENT_QUOTES));
$query->bindValue(':address-town',htmlspecialchars($address_town, ENT_QUOTES));
$query->bindValue(':address-country',htmlspecialchars($address_country, ENT_QUOTES));
var_dump($query);
$query->execute();
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>

can anyone see the error?谁能看到错误?

Hyphen "-" is a reserved character for pdo sql parser, so it must not be used in placeholder names, hence address-town and address-country are invalid names and cause the mentioned error.连字符“-”是 pdo sql 解析器的保留字符,因此不能在占位符名称中使用它,因此 address-town 和 address-country 是无效名称并导致上述错误。

Use alphanumeric characters and underscore only.仅使用字母数字字符和下划线。

a-zA-Z0-9_ a-zA-Z0-9_

Reserved characters:保留字符:

:?"'-/ :?"'-/

See the docs https://github.com/php/php-src/blob/master/ext/pdo/pdo_sql_parser.re请参阅文档https://github.com/php/php-src/blob/master/ext/pdo/pdo_sql_parser.re

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:消息中出现“ SQLSTATE [HY093]:无效的参数编号:未定义参数”的未捕获异常“ PDOException” - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in 致命错误:未捕获的 PDOException:SQLSTATE[HY093]:无效参数号:未定义参数 - Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined 致命错误:未捕获的 PDOException:SQLSTATE[HY093]:无效的参数号:无参数 - Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters 致命错误:带有消息&#39;SQLSTATE [HY093]的未捕获异常&#39;PDOException&#39;:参数号无效:没有参数被绑定 - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound 致命错误:未捕获的PDOException:SQLSTATE [HY093]:无效的参数编号:混合的命名和位置参数位于 - Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters in 致命错误:未捕获的 PDOException:SQLSTATE[HY093]:无效的参数号: - Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: PHP致命错误:消息为&#39;SQLSTATE [HY093]的未捕获异常&#39;PDOException&#39;:无效的参数编号: - PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: php 错误:致命错误:未捕获 PDOException:SQLSTATE[HY093]:无效参数号:绑定变量数与令牌数不匹配 - php error : Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens 致命错误:未捕获的异常 &#39;PDOException&#39; 带有消息 &#39;SQLSTATE[HY093]:参数无效 - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter 我如何消除此错误:PDOException:SQLSTATE [HY093]:无效的参数编号:参数未定义 - How i can remove this error: PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM