Jump to content

[SOLVED] Taking data from one table and inserting it into another


zhauan

Recommended Posts

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

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

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.