How To Insert/update Data Using $wpdb

In this tutorial, I will let you know insert, update and delete record in wordpress. The WordPress is very popular open source in PHP. WordPress has alot of inbuilt functionalities, which is basic for any website and blog.

Here. We will discuss how to insert,delete 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.

How To Delete Record using $wpdb

The $wpdb is having delete() method to delete the record from wordpress.

6 thoughts on “How To Insert/update Data Using $wpdb

    • 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).

  1. this is a mistake, the parentheses mislocated and form 2 parameters for update:
    $wpdb->update(‘table_name’, array(‘id’=>’1’, ‘name’=>’parvez’, array(‘Id’ => ‘2’)));
    should be
    $wpdb->update(‘table_name’, array(‘id’=>’1’, ‘name’=>’parvez’), array(‘Id’ => ‘2’));

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.