Jump to content

[SOLVED] Possible to grab the auto Incremending ID after creating the table row?


keldorn

Recommended Posts

I'm creating a content management system. In the back end I have using Ckeditor (Formally FCKeditor).

I have a large part of it done. When I hit "draft", I save the article into a draft table as a row with an incrementing ID number.

 

 

 mysql_query("INSERT INTO `drafts` (content,date,title) VALUES('$db_content','$date','$article_title')") or die(mysql_error()); 

 

After that I return all those variables back the CKeditor  so I can continue editing the article.

But If I hit "draft" again it will create a duplicate copy. So what I want to do is get the ID field and place it in a hidden field on the ckeditor and check for that during a post and then just update it again.

 

Here is my code example:

 

 

// fyi: Much other validation and stuff going on before here

// Is this a draft save?
if(isset($_POST['draft'])) {

                     // Check for the hidden field draft id, if its set then this is not a new draft 
                    //  but one that needs to be updated.
                     if(isset($_POST['draft_id']) && is_numeric($_POST['draft_id'])) {
                                  
                                     $draft_id = $_POST['draft_id'];
                      }
    
	  
	    // Check if the draft id is set, if it change our mysql query 
		// to just update the corresponding draft rather then creating a duplicate copy.
	    if(!empty($draft_id)) {

               mysql_query("UPDATE `drafts` SET content='{$db_content}', date='{$date}', title='{$article_title}' WHERE id={$draft_id}") or die(mysql_error());
		   $notice = "Your Draft has been updated";
		}
		else
		{
		   mysql_query("INSERT INTO `drafts` (content,date,title) VALUES('$db_content','$date','$article_title')") or die(mysql_error());
		   $notice = "Your Draft has been saved";

   
           /* To do: This is first Draft save so grab the Article incrementing ID HERE... */



		}

 

 

... So how I can get the ID on first draft save? I hope the logic of this makes sense. :shrug:

I know I could do another mysql query and maby just order it by ID then grab the first row. But that doesn't a good idea. What if there are 2 editors working saving drafts at the same time you know? You could get the wrong draft 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.