Jump to content

Archived

This topic is now archived and is closed to further replies.

quasiman

copy record from table1 to table2

Recommended Posts

I'm trying to copy all records in table1 to table2 (then verify and delete from table1, but I haven't gotten that far yet).

But table2 has additional columns, so I must specify each of the columns I want copied. 
Even doing so I'm getting the error "Column count doesn't match value count at row 1".

Here's what I'm trying:
[code=php:0]
<?php
include('includes/config.inc');

// Make the DB connection
$sql_link = mysql_connect ("$sql_host", "$sql_user", "$sql_pass")
or exit;
mysql_select_db("$sql_db");
//collect info
$result = mysql_query("SELECT 'id', 'time', 'name', 'address', 'subject_text', 'body_text' FROM table1");
//insert the info into the 2nd table
$move = "INSERT INTO mailbox (id, time, name, address, subject_text, body_text) VALUES ('$result')";
mysql_query($move) or die(mysql_error());
?>
[/code]

Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
Sounds like you want:
[code]
INSERT INTO mailbox (id, time, name, address, subject_text, body_text) SELECT 'id', 'time', 'name', 'address', 'subject_text', 'body_text' FROM table1
[/code]

Share this post


Link to post
Share on other sites
do you mean:
[code=php:0]
$move_message = "INSERT INTO mailbox (id, time, name, address, subject_text, body_text) SELECT 'id', 'time', 'name', 'address', 'subject_text', 'body_text' FROM table1";
mysql_query($move_message) or die(mysql_error());
[/code]

All this does is insert "id, time, name, address, subject_text, body_text" - not the actual records

Share this post


Link to post
Share on other sites
Try it without the single quotes. INSERT....SELECT syntax is explained here: [url=http://www.phpfreaks.com/mysqlmanual/page/manual_SQL_Syntax.html#INSERT_SELECT]http://www.phpfreaks.com/mysqlmanual/page/manual_SQL_Syntax.html#INSERT_SELECT[/url]

Share this post


Link to post
Share on other sites
Right, no string literals... I just blindly copied & pasted.. my bad.

Share this post


Link to post
Share on other sites

×

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.