Jump to content

[SOLVED] Retrieving PK from a Row Freshly Inserted


daniel244rock

Recommended Posts

Hello!

So I haven't had to post on PHPFreaks for Months and Months!! I must be improving, but obviously have a boatload yet to learn.  I have a script that copies a row from a database table into the same table, with all fields except the PK and a few others that are specific to the user entering the data.  The problem with this, is that the two rows (the one I copied from and the one newly created) are identical with the exception of the PK which is an auto-incrementing value. 

I need to be able to update a few columns with the user-specific data but don't know how to select the newly created row.  It seems that there should be a way you can find the PK when you do an INSERT statement.

For instance:

Tablename : table
PK : ID (Auto-increment)

----------------------------
|ID | name                       |
----------------------------
| 0 | Tommy                     |
----------------------------
| 1 | Timmy                      |
----------------------------
| 2 | James                      |
----------------------------

Then when you run "INSERT INTO table (name) VALUES (Jimmy)" have a way of getting the PK of 3.

If I'm confusing, then thats normal.  I'll try to answer any questions you guys have about my descriptions.
If its too much of  a dumb question, I'm not afraid to read some articles if you've got the links.
Thanks tons in advance. 

Daniel
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.