keldorn Posted September 17, 2009 Share Posted September 17, 2009 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. 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 https://forums.phpfreaks.com/topic/174617-solved-possible-to-grab-the-auto-incremending-id-after-creating-the-table-row/ Share on other sites More sharing options...
kickstart Posted September 17, 2009 Share Posted September 17, 2009 Hi The function you need is mysql_insert_id http://www.php.net/manual/en/function.mysql-insert-id.php All the best Keith Link to comment https://forums.phpfreaks.com/topic/174617-solved-possible-to-grab-the-auto-incremending-id-after-creating-the-table-row/#findComment-920245 Share on other sites More sharing options...
keldorn Posted September 17, 2009 Author Share Posted September 17, 2009 Wow that was fairly easy Kickstart. mysql_query("INSERT INTO `drafts` (content,date,title) VALUES('$db_content','$date','$article_title')") or die(mysql_error()); $draft_id = mysql_insert_id(); Thanks. Link to comment https://forums.phpfreaks.com/topic/174617-solved-possible-to-grab-the-auto-incremending-id-after-creating-the-table-row/#findComment-920261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.