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
Share on other sites

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();
Edited by MarioApprentice
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.