Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8405075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:45:30+00:00 2026-06-09T22:45:30+00:00

I am looping through a set of values $id and on certain occasions I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:45:31+00:00Added an answer on June 9, 2026 at 10:45 pm

    MySQL doesn’t support the SELECT INTO syntax.

    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:

    $copy_query = '
        INSERT INTO product_new (product_description, image)
        SELECT product_description, image
        FROM   product
        WHERE  product_id IN (' . implode(',', $ids) . ')';
    
    mysql_query($copy_query);
    

    Where $ids is 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_new and you want to update them, you can use:

    $copy_query = '
        UPDATE product_new a
        JOIN   product b ON a.product_id = b.product_id
                        AND b.product_id IN (' . implode(',', $ids) . ')
        SET    a.product_description = b.product_description,
               a.image = b.image';
    
    mysql_query($copy_query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find records in a dataset that have certain values from more
I am looping through an array of urls. Each one fetches some jsonp data
I'm having trouble looping through a set of dynamically created UITextFields and storing those
I'm trying to create an array, looping through another array and taking the string
I am looping through a big data file and like to detect the type
I am trying to extract a table of values from an excel (2003) spreadsheet
I am looking for a way to set permissions on a virtual directory through
Looping through each id retrieve value of objects in 1 dimentional array and 2
While looping through a Dataset which code snippet should I use, should I go
When looping through a DataRow and encountering types such as DataRow dr; dr[someString] dr[someInteger]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.