Log in

View Full Version : Creating Forums!



MrSheen
02-06-2008, 11:51 AM
Hey there, im creating some forums and the database tables are as follows.

forums_cat
forums_boards
forums_topics
forums_posts

When someone posts a new topic i am wanting this to happen:

In the forums_topics table i'm wanting the topic_ID, topic_subject and topic_poster to be stored.

I can do this fine but then it comes the problem when im trying to add to the posts table.

in the posts table im wanting to add post_id, TOPIC_ID.

How can i make it add the topic ID because it needs to get the ID from the topic just added.

james438
02-07-2008, 01:24 AM
I think I have an idea of what you are asking, but it would be very helpful if you could post the code that you are having trouble with so we can look at what you are trying to do.

jackbenimble4
02-07-2008, 08:38 PM
If you're using MySQL you can use the function mysql_insert_id (http://us3.php.net/mysql_insert_id). This function returns the id of the last insert query executed on the last link opened by mysql_connect(). Note: Your topic_id column needs to be auto incrementing for this function to retrieve it's value.

Here's an example:


$subject = 'Whatever';
$poster = 'Whoever';
// add the topic
mysql_query("INSERT INTO forums_topics (topic_subject, topic_poster) VALUES('".$subject."','".$poster."')");

$topicid = mysql_insert_id();

mysql_query("INSERT INTO forums_posts (topic_id) VALUES('".$topicid."')");

// if you wanted the value of the auto increment column post_id, you could use mysql_insert_id() again.

MrSheen
09-17-2008, 10:10 AM
Thanks thats great! :)