0

Apparently this...

$lastid = $wpdb->insert_id

...will give me the last inserted row (as noted here: How to get last inserted row ID from wordpress database?).

But how can I target a specific table, and get information from the latest inserted row after a form was just submitted?

For example, I have a table called 'license' and each row contains the columns 'email' and 'name' (among others).

The idea is that after I insert something into that row with a form, I need to display the email and name on screen.

Any help would be awesome.

Community
  • 1
  • 1
User_FTW
  • 504
  • 1
  • 16
  • 44

3 Answers3

0

You can use the value of an auto-increment column. You might already have the ID column doing so.

In a new SELECT-statment use ORDER BY myCol DESC the auto-increment-coloumn and use LIMIT 1 to only get the one with the highest value (wich is the most recent one)

CoderPi
  • 12,985
  • 4
  • 34
  • 62
0

According to what you already have you have to use the last inserted id for a new query.

$query = "select * from license where id = ".$lastid;

Using the result of this query you can print out the other information of the last inserted row.

If you need more info on the php code, provide your code so we can edit it accordingly

davejal
  • 6,009
  • 10
  • 39
  • 82
0

ID of the Last Inserted Record in general (in your example from Wordpress), can only fetch (as the name says) only the ID (AUTO_INCREMENT) from a column as a result of the most recently executed INSERT statement. mysql doc

There is no direct way if specifying "get me lastID of XYZ table" for a particular in reverse point in time, only works as described above.

Even with the proper usage in php you need to be careful if other INSERT statements on other tables (ones you don't want) are executed before you try to fetch LAST_INSERT_ID, so you don't end up getting IDs from unwanted tables.

Your possible workarounds:

  • do a SELECT from your desired table ordered by ID desc, e.g:

    SELECT id FROM xyz ORDER BY id DESC LIMIT 1;
    

    this is presuming id is an AUTO_INCREMENT or similar

  • another option is if possible in code locate the "position" of INSERT statements of the table you want e.g. xyz and fetch the id right there with:

    $wpdb->insert_id