Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: OO php problem

  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    Walsall, UK
    Posts
    38
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    'INSERT INTO' does exactly that, it inserts a new record into the db.

    To update a record, use UPDATE <table>
    John

  • Users who have thanked PappaJohn for this post:

    Kineas (11-25-2009)

  • #3
    New Coder
    Join Date
    Oct 2009
    Location
    Walsall, UK
    Posts
    38
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Last edited by tomws; 11-25-2009 at 02:03 AM.
    Are you a Help Vampire?

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • #6
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    $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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •