Getting the ID of the last inserted row in MySQL via PHP...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rcmatt4321
    New Member
    • May 2007
    • 25

    Getting the ID of the last inserted row in MySQL via PHP...

    It's comming along well! I have decided to add a admin page where somebody could edit or delete a comment. I am on the editing part. I have a problem though, I need to post the data to the next form without a form. Make sense?

    This is why.

    When you edit a comment the comment will appear in the text box, you will then change it and submit and it will post to the server over the left comment. So I need to post the number of the comment so it knows which one to relpace and the actual comment that you clicked the edit under.

    Thanks,
    Matt.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    I'm going to go ahead and split this thread so your question gets better exposure :)

    Originally posted by rcmatt4321
    When you edit a comment the comment will appear in the text box, you will then change it and submit and it will post to the server over the left comment. So I need to post the number of the comment so it knows which one to relpace and the actual comment that you clicked the edit under.
    So what you're looking for is the ID number of the previously-inserted comment. Is this correct?

    You'll want to take a look at mysql_insert_id for that one.

    Comment

    • rcmatt4321
      New Member
      • May 2007
      • 25

      #3
      No not really, I already have a collumn in my database where it does an autoincrement. I just need to get that info and the comment over to the other page so you can edit it in a text box. It would look like this.

      Hello!
      Matthew
      Hi, I like your page

      Edit Delete

      If you clicked on delete it would take that comment, the one you clicked to edit and take you to a new page where the comment would be posted inside a text box for you to edit, you click submit and it would replace the comment in the database, so I would need the number of the comment. I need to know how to post values without a form.

      Thanks for All The Help,
      Matt

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by rcmatt4321
        No not really, I already have a collumn in my database where it does an autoincrement. I just need to get that info and the comment over to the other page so you can edit it in a text box.
        Ah. So when a User submits a comment, you want to go to a new page that displays that comment, along with links allowing the User to edit and delete that comment.

        What I would do in this case would be to pass the id of the comment in the URL. E.g.:

        ThePageThatSave sTheComment.php :
        [code=php]
        header('Locatio n: comment_preview .php?commentid= ' . mysql_insert_id ());
        exit;
        [/code]

        comment_preview .php:
        [code=php]
        $comment = mysql_query("SE LECT * FROM `comments` WHERE `commentid` = '" . intval($_GET['commentid']) . "' LIMIT 1");
        .
        .
        .
        ?>
        <a href="comment_e dit.php?comment id=<?php echo $_GET['commentid']; ?>">Edit</a>
        [/code]

        Comment

        Working...