我正在尝试使用带有数组形式参数的简单MySQL插入查询。它不断告诉我参数的数量是错误的:
警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:参数未定义
这是我的代码
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'],
));
答案 0 :(得分:0)
当你在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'],
));