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

  • SEARCH
  • Home
  • 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 8935857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:05:14+00:00 2026-06-15T10:05:14+00:00

In short what I need to do is: Duplicate X amount of rows in

  • 0

In short what I need to do is:

  1. Duplicate X amount of rows in a table
  2. Alter a few fields
  3. Keep the auto incrementing IDs in order.

This is what I have going so far.

$this->db->query("CREATE TEMPORARY TABLE tmpTbl AS SELECT * FROM orders_products WHERE op_order_id = $id");
$temp = $this->getAll('tmpTbl');

## grab the highest id in the orders_products table so we can begin auto_inc after that
$this->db->select_max("op_id");
$max = $this->getAll('orders_products');
## set the id to a counter for the loop
$counter = $max[0]->op_id;

## loop through the results editing fields as needed
foreach ( $temp as $t ) :
  $counter++;
  $this->db->query("UPDATE tmpTbl SET op_id = $counter, op_order_id = $orderId WHERE op_order_id = $id");
endforeach;
$temp = $this->getAll('tmpTbl');

## insert the new duplications into the orders_products table
$this->db->query("INSERT INTO orders_products SELECT * FROM tmpTbl");

## drop the temp table so it is fresh and clean for the next duplication
$this->db->query("DROP TEMPORARY TABLE tmpTbl"); 

When I run it I get up to this line

$this->db->query("INSERT INTO orders_products SELECT * FROM tmpTbl");

and it throws me this error

Duplicate entry '54' for key 'PRIMARY

INSERT INTO orders_products SELECT * FROM tmpTbl

The reason I created the counter in the first place was to be a fix for this problem. However, I am sadly mistaken.

  • 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-15T10:05:15+00:00Added an answer on June 15, 2026 at 10:05 am

    What I would do In your case is to remove the ID col. from the SELECT so that the INSERT will generate automatically the ID.

    INSERT INTO orders_products (column2,column3,etc)
        SELECT column2, column3, etc FROM tmpTbl
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand we need to keep @Transactional boundaries as short as possible. Here is
(I shall try to keep this question as short as possible while clearly describing
Possible Duplicate: How to enable PHP short tags? Don't know how to search this
Possible Duplicate: Short 'encryption/hash' in PHP Right, I need to make an ID's e.g;
How can i shorter below codes than before? i need short and simple method:
The short version: I need to save mail from Exim to a networked directory,
I need to display short description like label and near it data associated with
I need to encrypt and decrypt short strings (Ex. 'product1234'). I have used mcrypt_encrypt
I need to use an error correcting technique on short messages (between 100 and
Short explanation of my problem, I need to parse an object and put its

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.