Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-24-2009, 11:36 PM   PM User | #1
Kineas
New Coder

 
Join Date: Oct 2009
Location: Walsall, UK
Posts: 38
Thanks: 7
Thanked 0 Times in 0 Posts
Kineas is an unknown quantity at this point
OO php problem

EDIT: I've coded a blog, and I want to be able to edit entries. A blog entry will have id, title, message, and date_added. I want the id to stay consistent when there has been an edit, so if you edit blog entry id 1, the updated version will also be id 1. I've got the following code, but it keeps throwing an error saying that it can't do it because there's a duplicate entry with the same id. This is my code:

ManagerMessage
PHP Code:
require_once("Manager.php");

class 
ManagerMessage extends Manager
{
  static 
$edit_message "INSERT INTO messages(id,title,message,date_added) values(?,?,?,?)";
  
  function 
addMessage($title$message)
  {
    
$values = array($id$title$messagedate("Y/m/d H:i:s"));
    
$this->doStatement(self::$add_message$values);
  } 
Command_BlogEdit
PHP Code:

class Command_BlogEdit extends Command
{

  function 
doExecute(Request $request)
  {
    
// Get data from request
    
$id $request->getProperty('editid');
    
$title $request->getProperty('edittitle');
    
$message $request->getProperty('editmessage');

    
// Create manager object
    
$manager = new ManagerMessage();    
    
    
// Add to database
    
$manager->editMessage($title$message$id);
    
    
// Redirect to index
    
header("location:blog.php");
  }


Last edited by Kineas; 11-24-2009 at 11:51 PM..
Kineas is offline   Reply With Quote
Old 11-24-2009, 11:48 PM   PM User | #2
PappaJohn
Senior Coder

 
Join Date: Apr 2007
Location: Quakertown PA USA
Posts: 1,028
Thanks: 1
Thanked 125 Times in 123 Posts
PappaJohn will become famous soon enough
'INSERT INTO' does exactly that, it inserts a new record into the db.

To update a record, use UPDATE <table>
__________________
John
PappaJohn is offline   Reply With Quote
Users who have thanked PappaJohn for this post:
Kineas (11-25-2009)
Old 11-25-2009, 12:10 AM   PM User | #3
Kineas
New Coder

 
Join Date: Oct 2009
Location: Walsall, UK
Posts: 38
Thanks: 7
Thanked 0 Times in 0 Posts
Kineas is an unknown quantity at this point
Thanks for your reply. I forgot to mention that I'm implementing versioning, so I don't actually want to update, but insert the editted entry, and then move the old version to a different table.
Kineas is offline   Reply With Quote
Old 11-25-2009, 01:59 AM   PM User | #4
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
Your database isn't going to let you do that. It sounds like you have the id field set appropriately as unique, possibly primary.

If you want to store revision within the same table, you'll need to edit that table config to remove the unique/primary from the field. Once you do that, you'll need to add some extra complexity to your blog post listing logic in order to pull out only the desired version.

An ideal system would probably separate revision storage from the master post storage. It would also add complexity, but would allow for cleanly separated data.

EDIT: I just read your last post again and understand now what you're saying. It still won't work for the previously mentioned reasons. But you probably can't just move/delete the old post and then add the revision. Depends on the id field.
__________________
Are you a Help Vampire?

Last edited by tomws; 11-25-2009 at 02:03 AM..
tomws is offline   Reply With Quote
Old 11-25-2009, 05:00 AM   PM User | #5
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Quote:
Originally Posted by Kineas View Post
Thanks for your reply. I forgot to mention that I'm implementing versioning, so I don't actually want to update, but insert the editted entry, and then move the old version to a different table.
You're doing it the wrong way around. Copy the existing version to the other table and then update the existing record with the new entry.
MattF is offline   Reply With Quote
Old 11-25-2009, 09:01 PM   PM User | #6
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
$this->doStatement(self::$add_message, $values);

should be

$this->doStatement(self::$edit_message, $values);
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:53 PM.


Advertisement
Log in to turn off these ads.