970_90

Sunday, 31 May 2015

Wordpress insert record into table and get last insert Id

Wordpress insert record into table and get last insert Id

In Wordpress, Record insertion into table is quite simple.
1. Create an Object of $wpdb;
2. Set the data in an Array.
3. Set the table name.
4. Use insert function  to insert record.
5. In insert function, first argument is table name and Second argument is array.


See Example Below:
global $wpdb;
$tableName = $wpdb->prefix . "users";
$saveFieldArray=array( 
'user_login' => 'mylogin',
'user_pass'=>'pass',
'user_nicename' => 'Poonam', 
'user_email' => [email protected]',
'user_registered' => date('Y-m-d H:i:s',time()),
'user_status' => '1',
'display_name' => 'Arun Kumar');

//Insert Into Database
$wpdb->insert( $tableName, $saveFieldArray);

//get the last inert Id
$lastInsertId = $wpdb->insert_id;  




No comments:

Post a Comment