2017-09-10 8 views
1

配列形式のパラメータを持つ簡単なMySQL挿入クエリを使用しようとしています。これは、パラメータの数が間違っている私に言って続けて:PHP - MySQLエラー:無効なパラメータ番号:パラメータが定義されていません

警告:PDOStatementに::実行():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'], 
    )); 
+1

次のようになりたいです。 – Qirel

+1

変数名に「アンダースコア」(_)を使用 –

+1

ダッシュを含む列名については、それぞれバッククォートを使用して引用符を付けます。 –

答えて

0

MySQLでハイフン(-)を使用すると、数学を実行していると考えられます。つまり、あるものを別のものから減算します。ハイフンで列名を使用すること自体は悪い考えですが、回避することは可能です。ただし、プレースホルダを変更する必要があります。有効なパターンは[:][a-zA-Z0-9_]+です。つまり、英数字の値とアンダースコアを使用できます。列の場合は、バッククックで囲む必要があります。それはあなたがMySQLの中で(やって数学)を引い以外の何のためにダッシュを使用しないでください。この

$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'], 
)); 
関連する問題