Jump to content

how do u move data from one table to another in a database


jbrill

Recommended Posts

hey guys,

just wondering what the mysql statment woudl look like if i wanted to transfer info from one table to another and then remove the info form the origional table.

 

basically, i have one table, "cartitems" it as productid, productnumber, qty, price.

on checkout i would like to transfer this exact info to another called "orders" with the same fields. then remove the info from "cartitems"

Link to comment
Share on other sites

First, I would query the cartitems table base on the productid.

 

$sql = "SELECT * FROM cartitems productid = 'the product id you want goes here'";

$results = mysql_query($sql) or die(mysql_error());

 

$data = mysql_fetch_array($results, MYSQL_ASSOC);

 

Then you will have the values you need to insert into the orders table such as

"INSERT INTO orders VALUES ('', $data["productnumber"], $data["qty"]... and so forth.)

 

Finally, you will delete from the cartitems table base on the original productid.  Hope this is what you were looking for.

Link to comment
Share on other sites

Yes your best bet is to script this, your script you will need to have two database connections using one for the select and the other for the insert.

 

Your other option is to move the actual table file.  This is only advisable (and really not advisable) if you don't mind losing whatever unique information is in the table your are overwriting.  It will require a database restart.

 

If I were you I would go with the script much as ainoy31 suggested.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.