
How to Insert Data into Table using WPDB
- February 12, 2016
- Leave a comment
WPDB is a WordPress class containing a set of functions to interact with Database. In this article, you will learn the use of WPDB‘s “insert” function that is used for inserting rows into tables.
Insert your values in the table by using the following method:
1 2 3 4 5 |
$wpdb->insert("pt_my_table_name",array( "name" => "PressTigers", "email"=>"example@email.com", "phone"=>"123456789" // ... and so on) ); |
Here, ‘pt_my_table_name’ is the name of table in which you want to insert the record. ‘name’, ‘email’ and ‘phone’ are the names of database columns. While ‘PressTigers’, ‘example@email.com’ and ‘123456789’ are the values for these columns respectively.
You can change the values of database table name and columns as per your requirement. Values can also be inserted using a PHP variable for dynamic values. You can use your variables to insert values in database in the following way as well:
1 2 3 4 5 |
$wpdb->insert("pt_my_table_name",array( "name" => $pt_name, "email"=>$pt_email, "phone"=>$pt_phone // ... and so on) ); |
Here $pt_name, $pt_email and $pt_phone are the variables containing values for the columns to be inserted.
THanks alot!!