Elven6 Posted September 6, 2010 Share Posted September 6, 2010 I want to take data from one table and insert it into another in the same database, the problem is the two tables have different values so it wouldn't be as simple as using an INSERT INTO script. From table 1 I want to extract a row ID as well as a field called systems. I then want to insert that into the second table which is structured, Id (null) Name (games) Value (the data given from Systems in the other) ID2 (the ID from the other table) Category (news) Any ideas how I can go about this? Quote Link to comment https://forums.phpfreaks.com/topic/212696-inserting-data-from-one-table-into-another-with-different-values/ Share on other sites More sharing options...
mraza Posted September 6, 2010 Share Posted September 6, 2010 something like this? $query = mysql_query("SELECT id,systems FROM table1"); while($row = mysql_fetch_array($query)) { mysql_query("INSET INTO table2 (`id2`, `value` ) VALUES ({$row['id']}, {$row['systems']})"); } Quote Link to comment https://forums.phpfreaks.com/topic/212696-inserting-data-from-one-table-into-another-with-different-values/#findComment-1107954 Share on other sites More sharing options...
Elven6 Posted September 6, 2010 Author Share Posted September 6, 2010 Quote something like this? $query = mysql_query("SELECT id,systems FROM table1"); while($row = mysql_fetch_array($query)) { mysql_query("INSET INTO table2 (`id2`, `value` ) VALUES ({$row['id']}, {$row['systems']})"); } Thanks for the code, I'll see if it works. One question though, what are the {} around things like $row['id'] for? Quote Link to comment https://forums.phpfreaks.com/topic/212696-inserting-data-from-one-table-into-another-with-different-values/#findComment-1107983 Share on other sites More sharing options...
mraza Posted September 7, 2010 Share Posted September 7, 2010 this {} is to separate variables from other code, so when using a variables inside a string etc, its always good practice to close variable inside {$variable} Quote Link to comment https://forums.phpfreaks.com/topic/212696-inserting-data-from-one-table-into-another-with-different-values/#findComment-1108163 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.