NathanLedet Posted April 13, 2009 Share Posted April 13, 2009 I have a little script I'm working with that has pages and page data (two MySQL tables) When you create a page, the database auto-increments the page ID number. I need to create a script that automatically generates a page and puts that page in the database - but the page data is in a separate table. So, my next step would be to create the page data and then the "page_id" needs to be the page id that was automatically created by MySQL So in my PHP/MySql - what's the best way to retrieve the ID of the entry that was just generated? Thanks! Quote Link to comment Share on other sites More sharing options...
trq Posted April 13, 2009 Share Posted April 13, 2009 mysql_insert_id Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted April 13, 2009 Author Share Posted April 13, 2009 Awesome - I'm going to give this a try $sql = "INSERT INTO page ('title', 'keywords', 'parent_id', 'status', 'created_by_id') VALUES('page title', 'key, word, page, title', '1', '1', '1') )"; $sql2 = "INSERT INTO page_body ('content', 'page_id') VALUES('Page Title', mysql_insert_id()) "; Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted April 13, 2009 Author Share Posted April 13, 2009 OK I typed it all out and this is my actual script. It is putting in a "0", when it should be 11 or 12 or whenever the previous query's id turned out to be: $PDO->exec("INSERT INTO `".TABLE_PREFIX."page`( `title`, `slug`, `breadcrumb`, `parent_id`, `status_id`, `created_by_id`, `updated_by_id`) VALUES( 'Tessst', 'Tessst', 'Tessst', '1', '100', '1', '1')"); $PDO->exec("INSERT INTO `".TABLE_PREFIX."page_part` ( `name`, `content`, `content_html`, `page_id`) VALUES('body', 'content goes here', '<p>content goes here</p>', '".mysql_insert_id()."')"); Quote Link to comment 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.