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

Link to comment
Share on other sites

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

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.