Jump to content

get ID of item just put in db


NathanLedet

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/153844-get-id-of-item-just-put-in-db/
Share on other sites

Awesome - I'm going to give this a try :D

 

$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())
";

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()."')");

Archived

This topic is now archived and is closed to further replies.

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