Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I am Richard, and i am a biginner in the PHP MYSQL codification. I'm developping a online site, and i am facing to a blocking point, regarding the insertion of products onsite.

I have 2 tables:

- 1 for User Registration (containning the primary key: userID)

- 1 for Products registration (containning the foreign key: userID).

This is a part of my product insertion request: "INSERT INTO shopping.produits (userID, productID, product_picture, product_name, product_desc, product_price) VALUES ($dataID, NULL, $product_picture, $product_name, $product_desc, $product_price)"

If i replace variables ($product_picture, $product_name, $product_desc, $product_price) in VALUES attribut, by putting '', and execute the request, everything works well, but, when i put variables, and execute the request, this is the message i get:

" Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\04 - Shopping\PHP\addProduct.php:67 Stack trace: #0 C:\xampp\htdocs\04 - Shopping\PHP\addProduct.php(67): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\04 - Shopping\PHP\addProduct.php on line 67 "

Line 67 concerns the array.

Could you please help me?

Thant you in advance.

Regards,

Richard

What I have tried:

$req_insert_product = $bdd->prepare("SELECT *
                           FROM produits
                           INNER JOIN form1
                           ON produits.userID=form1.userID");
                           $req_insert_product->execute();
                           $all_datas = $req_insert_product->fetch();
                           $dataID = $all_datas['userID'];


                           $req_insert_products = $bdd->prepare("INSERT INTO shopping.produits (userID, productID, product_picture, product_name, product_desc, product_price) VALUES ($dataID, NULL, $product_picture, $product_name, $product_desc, $product_price)");
                           $req_insert_products->execute(array(
                               'userID' => $dataID,
                               'productID' => NULL,
                               'product_picture' => $product_picture,
                               'product_name' => $product_name,
                               'product_desc' => $product_desc,
                               'product_price' => $product_price
                           ));
Posted
Updated 29-Mar-22 1:16am

The issue here is that you're mixing string variable interpolation with named parameters. In PDO statements you don't declare named parameters as $variable but instead as :variable. PHP: PDOStatement::execute - Manual[^]

You need to both change the named parameters to use a colon (:) and also make sure that the values you're passing into the execute() function also match the same names. In your code you're trying to use $dataID but you're passing in a value of 'userID', so these don't match. Below is the correct usage:

PHP
$req_insert_products = $bdd->prepare("INSERT INTO shopping.produits (userID, productID, product_picture, product_name, product_desc, product_price) VALUES (:userID, NULL, :product_picture, :product_name, :product_desc, :product_price)");

$req_insert_products->execute(array(
        'userID' => $dataID,
        'product_picture' => $product_picture,
        'product_name' => $product_name,
        'product_desc' => $product_desc,
        'product_price' => $product_price
));

Also note that I removed the 'productID' => NULL as this isn't actually being used in the query.
 
Share this answer
 
Comments
richard kasongo 29-Mar-22 7:18am    
$bdd is my data base connexion request.
Thank you for your help, this is what i did, and it works:


$req_getID = $bdd->prepare("SELECT *
                            FROM produits 
                            INNER JOIN form1 
                            ON produits.userID=form1.userID");
                            $req_getID->execute();
                            $all_datas = $req_getID->fetch();
                            $dataID = $all_datas['userID'];
                            

                            $req_insert_product = $bdd->prepare("INSERT INTO shopping.produits (userID, productID, product_category, product_picture, product_name, product_desc, product_price) VALUES (:dataID, :productID, :product_category, :product_picture, :product_name, :product_desc, :product_price)");
                            $req_insert_product->bindValue(':dataID', $dataID, PDO::PARAM_INT);
                            $req_insert_product->bindValue(':productID', NULL, PDO::PARAM_NULL);
                            $req_insert_product->bindValue(':product_category', $product_category, PDO::PARAM_STR);
                            $req_insert_product->bindValue(':product_picture', $product_picture, PDO::PARAM_INT);
                            $req_insert_product->bindValue(':product_name', $product_name, PDO::PARAM_STR);
                            $req_insert_product->bindValue(':product_desc', $product_desc, PDO::PARAM_STR_CHAR);
                            $req_insert_product->bindValue(':product_price', $product_price, PDO::PARAM_INT);
                            $req_insert_product->execute();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900