使用更改和自动递增ID重复数据库中的多行

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.

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