zhauan Posted September 9, 2008 Share Posted September 9, 2008 Hello, I apologize if this question has been answered before, but searching through nearly 17 pages was a little out of the question for my lazy eyes. First let me explain my situation. I am attempting to insert information into a table, from a different table. Since I'm fairly new to PHP and MySQL queries, I haven't a clue to what I should do heh. Here is much of my code (without the unneeded portions)... <?php $sql = "SELECT * FROM phpbb_users WHERE `user_id`>52"; $result = mysql_query($sql); $data = mysql_fetch_array($result); $sql2 = "SELECT * FROM phpbb_forums"; $result2 = mysql_query($sql2); $data2 = mysql_fetch_array($result2); $sql3 = "SELECT * FROM phpbb_posts"; $result3 = mysql_query($sql3); $data3 = mysql_fetch_array($result3); $insert = "INSERT INTO cms_users (user_id,username, user_email, user_regdate, commentmax, user_lastvisit, user_posts) VALUES ('$userid','$username','$email','$regdate','4096','$userlastvisit','$posts')"; mysql_query($insert); $truncate = "TRUNCATE TABLE cms_bbforums"; mysql_query($truncate); $insert2 = "INSERT INTO cms_bbforums (forum_id, forum_name, forum_desc, forum_posts, forum_topics, forum_last_post_id, prune_enable, prune_next) VALUES ('$forumid,'$forumnames','$forumdesc','$forumposts','$forum_topics','$forumlastpostid','$enableprune','$prunenext')"; mysql_query($insert2); $insert3 = "INSERT INTO cms_bbposts (post_id, topic_id, forum_id, poster_id, post_time, enable_bbcode, enable_smiles, enable_sig, post_edit_time, post_edit_count) VALUES ('$postid','$topic_id','$forumids','$posterid','$posttime','$enablebbcode','$enablesmiles','$enablesig','$postedittime','$posteditcount')"; $insert4 = "INSERT INTO cms_bbposts_texts (post_id,post_subject,post_text) VALUES ('$postid','$postsubject','$posttext')"; ?> Thanks Much, Zach Quote Link to comment https://forums.phpfreaks.com/topic/123394-solved-taking-data-from-one-table-and-inserting-it-into-another/ Share on other sites More sharing options...
cooldude832 Posted September 9, 2008 Share Posted September 9, 2008 You can model a single query to do off this INSERT INTO `TABLE1` AS T1, `TABLE2` as T2 ( T1.Field1, T2.Field2, T1.Field2 ) SELECT UserID, Email, Phone FROM `users` Quote Link to comment https://forums.phpfreaks.com/topic/123394-solved-taking-data-from-one-table-and-inserting-it-into-another/#findComment-637694 Share on other sites More sharing options...
zhauan Posted September 11, 2008 Author Share Posted September 11, 2008 I apologize for not viewing this earlier, been really busy with class. So essentially have the code formed like this? <?php $sql =" INSERT INTO `cms_users` AS T1, ( T1.user_id, T1.username, T1.user_email, T1.user_posts, ) SELECT user_id, username, user_email, user_posts, FROM `phpbb_users` WHERE `user_id`>52 "; $result = mysql_query($sql); $data = mysql_fetch_row($result); ?> And when I get two tables that need data from one table: <?php $sql =" INSERT INTO `cms_bbtopics` AS T1, `cms_bbposts` AS T2, `cms_bbposts_text` AS T3 ( T1.topic_id, T1.forum_id, T1.topic_title, T1.topic_poster, T1.topic_time, T2.post_id, T2.topic_id, T3.post_id, ) SELECT `topic_id`, `forum_id`, `post_subject`, `user_posts`, `post_time`, `post_id`, `topic_id`, `post_id` FROM `phpbb_posts` "; $result = mysql_query($sql); $data = mysql_fetch_row($result); ?> So, when I run into a table that needs the same data in a field, such as the post_id up above, I can just SELECT it again, and insert it into the second table? Thanks again, Zach Quote Link to comment https://forums.phpfreaks.com/topic/123394-solved-taking-data-from-one-table-and-inserting-it-into-another/#findComment-639200 Share on other sites More sharing options...
zhauan Posted September 12, 2008 Author Share Posted September 12, 2008 Well after a few attempts, I got the code to work properly! Thank you for your time and expertise cooldude832, Zach Quote Link to comment https://forums.phpfreaks.com/topic/123394-solved-taking-data-from-one-table-and-inserting-it-into-another/#findComment-640166 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.