How To Insert/update Data Using $wpdb

WordPress is very popular open source in PHP. WordPress has number inbuilt functionality which is baseic for any website and blog.Here we will discuss how to insert and update record using wordpress connection object. WordPress defines a class called wpdb, which contains a set of functions used to interact with a database. Its primary purpose is to provide an interface with the WordPress database, but can be used to communicate with any other appropriate database.

There are two methods to access $wpdb object:

1- $wpdb : Declared $wpdb as global and used it to execute an SQL query statement that returns a PHP object.

2- $GLOBALS : Access $GLOBALS superglobal. Does not require global keyword (but may not be best practice).

How to insert record using $wpdb:

We can use insert method to insert data into database.

How to update record using $wpdb:

We can use update method to insert data into database.

  • Bhumi

    nice post but it would be great if you can explaing $wpdb->insert and $wpdb->update functions parameters.

    • Sure,
      $wpdb->insert function takes two parameters(table name and column values) and $wpdb->update function takes three parameters (table name, value and id-which row to update).

  • GemmaNorris

    Great post.