Jump to content

Getting an auto-increment id?


LemonInflux

Recommended Posts

OK, I am building my forum, as people know, and have hit a problem. Posting..

 

I can work posting out just fine, that's not a problem. The problem is, however, as follows:

 

When someone creates a topic, the following is executed:

 

$sql = mysql_query("INSERT INTO `reflexproj`.`topics` (`topic_name`, `topic_desc`, `poster`, `forumid`) VALUES ('". $posttitle ."', '". $desc ."', '". $sessionu ."', '". $forum ."');");

 

This inserts the topic into the topic table. However, then this is executed:

 

$sql = mysql_query("INSERT INTO `reflexproj`.`posts` (`poster`, `post`, `topicid`) VALUES ('". $sessionu ."', '". $fp ."' '". $id ."');");

 

This is meant to insert a post into the posts table that starts the topic. However, this is a problem. The problem is, that $id hasn't been defined.

 

My question is this, really: How do I get $id? Basically, I've got to find a way of getting an auto_increment value from a database. Anyone any ideas?

Link to comment
Share on other sites

Sounds like you know what your doing, however, this may be what you need.

 

Auto-incremental ID's are setup in the database directly.  If you are using phpMyAdmin to configure your database make sure that your ID column is set to INT and Extra is set to auto-increment.  Doing this will automatically populate the ID field for you.  Each new record inserted will elevate the ID number by one each time.

 

Be sure to check and make sure that your queries contain the same number of columns that your database has.  You can always echo your queries so you can get a visual confirmation of your success.

Link to comment
Share on other sites

<?php
$sql = mysql_query("INSERT INTO `forum`.`topics` (`topic_name`, `topic_desc`, `poster`, `forumid`) VALUES ('". $posttitle ."', '". $desc ."', '". $sessionu ."', '". $forum ."');");
$sql = mysql_query("INSERT INTO `forum`.`posts` (`poster`, `post`, `topicid`) VALUES ('". $sessionu ."', '". $fp ."' '". mysql_insert_id() ."');");
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.