Jump to content

bindValue() with INSERT and inner SELECT


Recommended Posts

I have a code that has to save a more than one reservation into a database.

This is the code

$sql = 'INSERT INTO reservations(name, price, user_id, proizvod, num_items, taken, date)
	          VALUES(
			       (SELECT name FROM product WHERE id=:id),
				   (SELECT price FROM product WHERE id=:id),
				   :user_id,
				   (SELECT id FROM product WHERE id=:id),
				   :num_items,
				   :taken,
				   :date
				 )';

$stmt->bindValue('user_id', 5, PDO::PARAM_INT);
/*
   same for the rest of the binded values.
*/

But it says that the 'number of binded variables does not match number of tokes' which is not true. I have       checked it 256789521 times in the past hour like some kind of crazy person. The manual doesn't say anything about partial binding of items to be inserted. Does anyone know what the problem is?

Link to comment
https://forums.phpfreaks.com/topic/279829-bindvalue-with-insert-and-inner-select/
Share on other sites

  On 7/3/2013 at 2:19 PM, mac_gyver said:

you need to post the actual code you want help with. otherwise how could someone possibly tell you what is wrong with it?

 

well its the sam principle and its on croatian so i had to translate it to make it more readable for you, but if it help, here it is, the same code on english. If i didn't put a colon there somewhere, its a mistake in the post, not in the actual code.

$sql = 'INSERT INTO reservations(name, price, user_id, product_id, number_of_items, taken, datum_postavljanja)
	          VALUES(
			           (SELECT name FROM product WHERE id=:id),
				   (SELECT price FROM product WHERE id=:id),
				   :user_id
				   (SELECT id FROM product WHERE id=:id),
				   :number_of_items,
				   :taken,
				   :date_of_request
				 )';

$stmt->bindValue(':user_id', $_SESSION['user']->getField('id'), PDO::PARAM_INT); // this is an object containg data from logged in user
$stmt->bindValue(':number_of_items', $komadi[$counter], PDO::PARAM_INT);  // index counter for the number of items user whats to order
$stmt->bindValue(':taken', 1, PDO::PARAM_INT);
$stmt->bindValue(':date_of_request', date('Y-m-d'), PDO::PARAM_STR);
			   
$stmt->execute();
  On 7/3/2013 at 2:48 PM, mac_gyver said:

you are not binding anything for the :id placeholder. you have 5 placeholders in the query and only 4 bind statements.

 

oh god, its an embarement. and this is the third time on this forum. sorry for the lost time. i shall go into the nearest corner and cry.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.