Php

How To Insert/update Data Using $wpdb

In this tutorial, I will let you know insert, update and delete record in wordpress. WordPress is a very popular open source in PHP. WordPress has a lot 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.

global $wpdb;
$results = $wpdb->function

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

Related Post
$results = $GLOBALS['wpdb']->function

How to insert record using $wpdb:

We can use the insert method to insert data into the database.

$wpdb->insert('table_name', array('id'=>'1', 'name'=>'parvez'));

How to update record using $wpdb:

We can use the update method to insert data into a database.

$wpdb->update('table_name', array('id'=>'1', 'name'=>'parvez', array('id' => '2')));

How To Delete Record using $wpdb

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

wpdb::delete('table', array( 'ID' => 1 ), array( '%d' ) )

View Comments

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

  • 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'));

  • It has been a while but it would be a bit clearer if we saw what was in the table before and then what was in it after the update.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories