gpt4 book ai didi

PHP-MySQL 错误 : Invalid parameter number: parameter was not defined

转载 作者:行者123 更新时间:2023-11-30 21:53:31 24 4
gpt4 key购买 nike

我正在尝试使用带有数组形式参数的简单 MySQL 插入查询。它一直告诉我参数数量错误:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

这是我的代码

try
{
$bdd = new PDO('mysql:host=localhost;dbname=looktallshoes;charset=utf8',
'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}

$req = $bdd -> prepare("INSERT INTO products (product-title, product-
category, product-source, source-link, product-price, price-before-discount,
product-source-price, height-increase, admin-product-short-description,
admin-product-long-description, large-main-name, square-main-name, other-
photo-1-name, other-photo-2-name, other-photo-3-name, other-photo-4-name)

VALUES(:product-title, :product-category, :product-source, :source-link,
:product-price, :price-before-discount, :product-source-price, :height-
increase, :admin-product-short-description, :admin-product-long-description,
:large-main-name, :square-main-name, :other-photo-1-name, :other-photo-2-
name, :other-photo-3-name, :other-photo-4-name)");

$req->execute(array(
'product-title'=>$_POST['product-title'],
'product-category'=>$_POST['product-category'],
'product-source'=>$_POST['product-source'],
'source-link'=>$_POST['source-link'],
'product-price'=>$_POST['product-price'],
'price-before-discount'=>$_POST['price-before-discount'],
'product-source-price'=>$_POST['product-source-price'],
'height-increase'=>$_POST['height-increase'],
'admin-product-short-description'=>$_POST['admin-product-short-
description'],
'admin-product-long-description'=>$_POST['admin-product-long-
description'],
'large-main-name'=>$_POST['large-main-name'],
'square-main-name'=>$_POST['square-main-name'],
'other-photo-1-name'=>$_POST['other-photo-1-name'],
'other-photo-2-name'=>$_POST['other-photo-2-name'],
'other-photo-3-name'=>$_POST['other-photo-3-name'],
'other-photo-4-name'=>$_POST['other-photo-4-name'],
));

最佳答案

当您在 MySQL 中使用连字符 (-) 时,它会认为您在做数学运算 - 从一个事物中减去另一个事物。因此,使用带有连字符的列名本身就是一个坏主意,但可以变通。但是,您的占位符需要更改。有效模式是 [:][a-zA-Z0-9_]+,这意味着您可以使用字母数字值和下划线。对于列,它们需要用反引号括起来。它看起来像这样

$req = $bdd->prepare("INSERT INTO products (`product-title`, `product-
category`, `product-source`, `source-link`, `product-price`, `price-before-discount`,
`product-source-price`, `height-increase`, `admin-product-short-description`,
`admin-product-long-description`, `large-main-name`, `square-main-name`, `other-
photo-1-name`, `other-photo-2-name`, `other-photo-3-name`, `other-photo-4-name`)

VALUES(:product_title, :product_category, :product_source, :source_link,
:product_price, :price_before_discount, :product_source_price, :height_increase,
:admin_product_short_description, :admin_product_long_description,
:large_main_name, :square_main_name, :other_photo_1_name, :other_photo_2_name,
:other_photo_3_name, :other_photo_4_name)");

$req->execute(array(
'product_title' => $_POST['product-title'],
'product_category' => $_POST['product-category'],
'product_source' => $_POST['product-source'],
'source_link' => $_POST['source-link'],
'product_price' => $_POST['product-price'],
'price_before_discount' => $_POST['price-before-discount'],
'product_source_price' => $_POST['product-source-price'],
'height_increase' => $_POST['height-increase'],
'admin_product_short_description' => $_POST['admin-product-short-description'],
'admin_product_long_description' => $_POST['admin-product-long-description'],
'large_main_name' => $_POST['large-main-name'],
'square_main_name' => $_POST['square-main-name'],
'other_photo_1_name' => $_POST['other-photo-1-name'],
'other_photo_2_name' => $_POST['other-photo-2-name'],
'other_photo_3_name' => $_POST['other-photo-3-name'],
'other_photo_4_name' => $_POST['other-photo-4-name'],
));

关于PHP-MySQL 错误 : Invalid parameter number: parameter was not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46141706/

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