I am looping through a set of values $id and on certain occasions I need to copy 2 fields of data from one table (product) to another (product_new). Both tables already exist and both contain identical fields ‘product_description’ and ‘image’. I am trying to do this with:
$copy_query = "SELECT product_description, image INTO product_new FROM product WHERE product_id=$id";
$result = mysql_query($copy_query) or die('Could not copy the database data: ' . mysql_error() . '<br>Query: ' . $copy_query);
The error is ‘Undeclared variable: product_new’. I don’t understand this though because I perform other operations with table product_new without issue. Any thoughts? Thanks.
UPDATE
Thanks for the link to the differences doc. That definitely helped, however I have one more issue. My current query is:
$copy_query = "INSERT INTO product_new (product_description, image)
SELECT product.product_description, product.image
FROM product
WHERE product_id=$id";
but this is writing the data to a new row with product_id = 0. I need it to write to the row with product_id = $id.
MySQL doesn’t support the
SELECT INTOsyntax.Also, it’s generally very inefficient to run queries inside of a loop. Instead, you can make a comma separated list of the
ids in PHP and use:Where
$idsis the array of your ids.This solution needs only to be executed once (rather than many times within a loop).
EDIT: If you already have rows that exist in
product_newand you want to update them, you can use: