Jump to content

Select and Insert


webmusic

Recommended Posts

Trying the following without success.

 

Need to pull several fields from 2 tables

and insert them into another table.

 

INSERT INTO $table3 SELECT field1,field2,field3 FROM $table1,$table2 WHERE $table1.id=$table2.member_id

 

field 1 is from table1, fields 2 and 3 are from table 2.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/1111-select-and-insert/
Share on other sites

[php:1:6ae32ffd94]$result = mysql_query ( \"SELECT $table1.field1, $table2.field2, $table2.field3 FROM $table1 INNER JOIN $table2 ON $table1.id = $table2.member_id\" );

 

if ( !$result )

{

// Fail

}

else

{

$row = mysql_fetch_assoc ( $result );

$field1 = $row[\'field1\'];

$field2 = $row[\'field2\'];

$field3 = $row[\'field3\'];

 

$sql = \"INSERT INTO $table3 ( field1, field2, field3 ) VALUES ( \'$field1\', \'$field2\', \'$field3\' )\";

 

$result = mysql_query ( $sql );

 

if ( mysql_affected_rows() > 0 )

{

// Success

}

else

{

// Fail

}

}[/php:1:6ae32ffd94]

 

That should get you started.

Link to comment
https://forums.phpfreaks.com/topic/1111-select-and-insert/#findComment-3774
Share on other sites

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.