Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: May 2007
Posts: 25
#1: May 24 '07
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's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: May 24 '07

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


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

Quote:

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.
Newbie
 
Join Date: May 2007
Posts: 25
#3: May 24 '07

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


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
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: May 24 '07

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


Quote:

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.:

ThePageThatSavesTheComment.php:
Expand|Select|Wrap|Line Numbers
  1. header('Location: comment_preview.php?commentid=' . mysql_insert_id());
  2. exit;
  3.  
comment_preview.php:
Expand|Select|Wrap|Line Numbers
  1. $comment = mysql_query("SELECT * FROM `comments` WHERE `commentid` = '" . intval($_GET['commentid']) . "' LIMIT 1");
  2. .
  3. .
  4. .
  5. ?>
  6. <a href="comment_edit.php?commentid=<?php echo $_GET['commentid']; ?>">Edit</a>
  7.  
Reply