LemonInflux Posted October 18, 2007 Share Posted October 18, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/ Share on other sites More sharing options...
brettpower Posted October 18, 2007 Share Posted October 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372510 Share on other sites More sharing options...
LemonInflux Posted October 18, 2007 Author Share Posted October 18, 2007 I have an ID column..I'm trying to get that id. Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372514 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2007 Share Posted October 18, 2007 to get the id of the last inserted record use mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372516 Share on other sites More sharing options...
LemonInflux Posted October 18, 2007 Author Share Posted October 18, 2007 I was trying to think of another way around it. If, by an offchance, 2 topics were posted at the same time, wouldn't you end up posting in different topics? Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372520 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2007 Share Posted October 18, 2007 every id will be unique. when you insert a record, you'll get the id of the record you inserted, not the id of some other record. Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372523 Share on other sites More sharing options...
brettpower Posted October 18, 2007 Share Posted October 18, 2007 Is the ID field in the database being populated? Or is the ID field blank in the database? Are you just trying to retrieve it? Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372525 Share on other sites More sharing options...
LemonInflux Posted October 18, 2007 Author Share Posted October 18, 2007 The ID is auto increment. I'm trying to find out what it is as I submit the information to the database. If it isn't possible, I'll use BlueSkyIS's idea. Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372528 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2007 Share Posted October 18, 2007 unless you specify the id yourself, you don't know what it is until after the record is inserted. after the record is inserted, use mysql_insert_id() to see what value the id was assigned by MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-372531 Share on other sites More sharing options...
LemonInflux Posted October 19, 2007 Author Share Posted October 19, 2007 I tried this idea, but there was a problem. mysql_insert_id always seems to return '1' Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-373510 Share on other sites More sharing options...
AndyB Posted October 19, 2007 Share Posted October 19, 2007 I tried this idea, but there was a problem. mysql_insert_id always seems to return '1' ... show us the code you're using to retrieve the value. Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-373512 Share on other sites More sharing options...
LemonInflux Posted October 19, 2007 Author Share Posted October 19, 2007 <?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() ."');"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-373522 Share on other sites More sharing options...
BlueSkyIS Posted October 19, 2007 Share Posted October 19, 2007 I tried this idea, but there was a problem. mysql_insert_id always seems to return '1' are you using the function with the parentheses? mysql_insert_id(); also, you should know to put or die(mysql_error()) after your mysql_query() calls! you have bad SQL. Quote Link to comment https://forums.phpfreaks.com/topic/73835-getting-an-auto-increment-id/#findComment-373524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.