JustPaste.it

How to insert data using wpdb - php?

how to insert data into wordpress database?

 

Using the $wpdb->insert()

WordPress has another way of inserting into the database. This is not your usual using of INSERT INTO statement.

The basic syntax for inserting data to WordPress database is <?php $wpdb->insert($table_name, $data); ?>. The $table_name is a string that is the name of the database table to insert data into. On the other hand, $data is an array that will be inserted into the database table.

Now we have learned the basic syntax for the WordPress insert, we should have a code like this:

 

  1. function myFunction() {
  2. global $wpdb;
  3. $table_name = $wpdb->prefix . 'my_table';
  4.  
  5. $wpdb->insert($table_name, array('column_1' => $data_1, 'column_2' => $data_2, //other columns and data (if available) ...));
  6. }