[英]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.