Jump to content

copy record from table1 to table2


quasiman

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!
Link to comment
https://forums.phpfreaks.com/topic/15556-copy-record-from-table1-to-table2/
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
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]

Archived

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

×
×
  • 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.