PHP - is it possible to make an INSERT INTO with php variables and more SELECT?

Asked

Viewed 371 times

0

I need to take information from another table and add it into another table with more information from a php variable I’ve been doing some research and some shapes I found was:

INSERT INTO tabProdutos ( id, descricao, unidade, qtd, valor) VALUES (‘$id’, ‘
(SELECT cod, descricao, unidade, qtd, valor FROM tabInfoProd WHERE cod = $cod ’);

But nothing happened

What could be wrong? Thank you.

  • 1

    Your query works by testing directly in the database?

  • Yes it is possible but this syntax does not seem correct. that $id after VALUES is left over in select.

1 answer

2


Your number of arguments declared in values is incorrect, try something like:

INSERT INTO tabProdutos ( id, descricao, unidade, qtd, valor) VALUES (
SELECT cod, descricao, unidade, qtd, valor FROM tabInfoProd WHERE cod = '$cod' );

or

INSERT INTO tabProdutos ( id, descricao, unidade, qtd, valor) VALUES (
SELECT '$id', descricao, unidade, qtd, valor FROM tabInfoProd WHERE cod = '$cod');
  • Hi thanks, but is showing error 1241: operand should contain 1 column

  • right, remove the select parentheses

  • edited the answer, after that should work

  • If it doesn’t work, try INSERT INTO tabProducts ( id, Description, unit, Qtd, value) SELECT '$id', Description, unit, Qtd, value FROM tabInfoProd WHERE Cod = '$Cod';

  • It worked just to remove the parentheses, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.