SyncViews Posted April 17, 2008 Share Posted April 17, 2008 1) I need to insert some data into two tables. The problem is the two records need to "point" to each other... <?php mysql_query("INSERT INTO topics (date, title, forum_id) VALUES ($date, '$subject', $_GET[forum]) ")or exit(mysql_error()); $topic_id = mysql_insert_id(); mysql_query("INSERT INTO posts (date, content, topic_id, user_id) VALUES ($time, '$content', $topic_id, $user_id) ")or exit(mysql_error()); $post_id = mysql_insert_id(); mysql_query("UPDATE topics SET first=$post_id WHERE topic_id=$topic_id")or exit(mysql_error()); ?> I tried doing this but it seems it's not allowed.. <?php ... mysql_query(" INSERT INTO topics, posts (topics.date, topics.title, topics.forum_id, topics.first, posts.topic_id, posts.user_id, posts.content, posts.date) VALUES ($time, '$subject', $_GET[forum], posts.post_id, topics.topic_id, $user_id, '$content', $time) ")or exit(mysql_error()); ... ?> Do I really need to make 3 seprate querys? 2)This makes loads of querys I'd rather not... <?php ... $con = mysql_connect($sql_host, $sql_user, $sql_pass) or exit('Failed to connect to database: ' . mysql_error()); $con2 = mysql_connect($sql_host, $sql_user, $sql_pass) or exit('Failed to connect to database: ' . mysql_error()); mysql_select_db($sql_db, $con2); $con3 = mysql_connect($sql_host, $sql_user, $sql_pass) or exit('Failed to connect to database: ' . mysql_error()); mysql_select_db($sql_db, $con3); //get sections $result1 = mysql_query(" SELECT section_id, section_name FROM sections ORDER BY display ASC ",$con)or exit(mysql_error($con)); for($i=0; $row1 = mysql_fetch_array($result1); ++$i) { $sections[$i]["DATA"]['ID'] = $row1["section_id"]; $sections[$i]["DATA"]['NAME'] = $row1["section_name"]; //get forums $result2 = mysql_query(" SELECT forum_id, forum_name, forum_desc, forum_topics, forum_posts FROM forums WHERE section_id=$row1[section_id] ORDER BY display ASC ", $con2) or exit(mysql_error($con2)); for($i2=0; $row2 = mysql_fetch_array($result2); ++$i2) { $sections[$i]["FORUMS"][$i2]["ID"] = $row2["forum_id"]; $sections[$i]["FORUMS"][$i2]["NAME"] = $row2["forum_name"]; $sections[$i]["FORUMS"][$i2]["DESC"] = $row2["forum_desc"]; $sections[$i]["FORUMS"][$i2]["TOPICS"] = $row2["forum_topics"]; $sections[$i]["FORUMS"][$i2]["POSTS"] = $row2["forum_posts"]; //get last post data if($row2["forum_topics"]) { $sections[$i]["FORUMS"][$i2]["LAST"] = true; $result3 = mysql_query(" SELECT users.user_id, users.username, posts.date, topics.topic_id, topics.title FROM users, posts, topics WHERE topics.forum_id=$row2[forum_id] AND topics.topic_id=posts.topic_id AND posts.user_id=users.user_id ORDER BY posts.date DESC LIMIT 0,1 ", $con3) or exit(mysql_error($con3)); $row3 = mysql_fetch_array($result3); $sections[$i]["FORUMS"][$i2]["LAST_TOPIC_ID"] = $row3["topic_id"]; $sections[$i]["FORUMS"][$i2]["LAST_TOPIC_NAME"] = $row3["title"]; $sections[$i]["FORUMS"][$i2]["LAST_TIME"]= date("M j Y, h:i A",$row3["date"]); $sections[$i]["FORUMS"][$i2]["LAST_USER_ID"] = $row3["user_id"]; $sections[$i]["FORUMS"][$i2]["LAST_USER_NAME"] = $row3["username"]; } else $sections[$i]["FORUMS"][$i2]["LAST"] = false; } } $smarty->assign ('SECTIONS', $sections); ... ?> Link to comment https://forums.phpfreaks.com/topic/101532-there-must-be-a-better-way/ Share on other sites More sharing options...
zenag Posted April 17, 2008 Share Posted April 17, 2008 it has to be..... mysql_query("INSERT INTO topics (date, title, forum_id) VALUES ($date, '$subject', $_GET[forum]) ")or exit(mysql_error()); ob_start(); $topic_id = mysql_insert_id(); mysql_query("INSERT INTO posts (date, content, topic_id, user_id) VALUES ($time, '$content', $topic_id, $user_id) ")or exit(mysql_error()); ob_flush(); Link to comment https://forums.phpfreaks.com/topic/101532-there-must-be-a-better-way/#findComment-519343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.