Arbitus Posted December 14, 2008 Share Posted December 14, 2008 So I'm working on this forum and I hit a problem. Right now I have 2 tables for posts and topics. I have a topics table which stores the board_id, topic_title, topic_owner, and of course, the topic ID itself. Then I have the posts table which also stores the topic_title, it's own id, users message and so on // Insert the information into arbi_topics first $newtopicsql = "INSERT INTO `arbi_topics` (title,board_id,starter_id) VALUES ('$topictitle','$boardid','$username')" or die (mysql_error()); mysql_query($newtopicsql) or die (mysql_error()); //Insert the information in arbi_posts second $newpostsql = "INSERT INTO 'arbi_posts' (topic_id,topic_title,message,post_owner) VALUES ('$newID','$topictitle','$topicmessage','$username')" or die (mysql_error()); In the first query, it is storing some information in the topic table. While the second query is storing the information in the posts table. Some how I need to get the ID from the topics table into a variable so I can store the topic ID into the posts table. Link to comment https://forums.phpfreaks.com/topic/136905-getting-id-after-submission/ Share on other sites More sharing options...
revraz Posted December 14, 2008 Share Posted December 14, 2008 Are you referring to the last ID inserted ? http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html Link to comment https://forums.phpfreaks.com/topic/136905-getting-id-after-submission/#findComment-715013 Share on other sites More sharing options...
Arbitus Posted December 14, 2008 Author Share Posted December 14, 2008 I can't figure out how to use it, or store it's value in a variable so I can insert it into the next query. // This is the topic table $newtopicsql = "INSERT INTO `arbi_topics` (title,board_id,starter_id) VALUES ('$topictitle','$boardid','$username')" or die (mysql_error()); mysql_query($newtopicsql) or die (mysql_error()); //I need the ID of what I just entered able to be inserted here in topic_id $newpostsql = "INSERT INTO 'arbi_posts' (topic_id,topic_title,message,post_owner) VALUES ('$topicid2','$topictitle','$topicmessage','$username')" or die (mysql_error()); mysql_query($newpostsql) or die (mysql_error()); Link to comment https://forums.phpfreaks.com/topic/136905-getting-id-after-submission/#findComment-715021 Share on other sites More sharing options...
mmarif4u Posted December 14, 2008 Share Posted December 14, 2008 you can use it like: $topic_id = mysql_insert_id(); if topic_id is auto_increment in topic table. Then save that $topic_id in arbi_posts. Link to comment https://forums.phpfreaks.com/topic/136905-getting-id-after-submission/#findComment-715025 Share on other sites More sharing options...
Arbitus Posted December 14, 2008 Author Share Posted December 14, 2008 I stumbled across MAX(ID) so I used that. Would it be better to use the mysql_insert_id() ? or is MAX(ID) alright. It seems to work beautifully Link to comment https://forums.phpfreaks.com/topic/136905-getting-id-after-submission/#findComment-715032 Share on other sites More sharing options...
mmarif4u Posted December 14, 2008 Share Posted December 14, 2008 Max will pickup the maximum id form the table, which can be any one (depends). But mysql_insert_id will pickup id, which is inserted just now. You can use MAX, its upto u. but better use mysql_insert_id. Link to comment https://forums.phpfreaks.com/topic/136905-getting-id-after-submission/#findComment-715033 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.