How to create database table, add data, update and delete using wpdb via plugins?

I am just starting out to create plugins with database. I know to create a custom database when activating the plugin. I just want to know whether we can create tables at the front end like adding the field name, field type. And also I tried creating a row values, but it seems that the values are not getting inserted in the table.
Here’s my code for adding values to the tables.

 function test_get_log_table_columns(){
return array(
    'log_id'=> '%d',
    'user_id'=> '%d',
    'activity'=>'%s'
);
}
 function insert_data_into_table(){
  global $wpdb;
 $logid=$_POST['logid'];
 $userid=$_POST['userid'];
//Set default values
 $data = wp_parse_args($data, array(
             'user_id'=> get_current_user_id(),
             'date'=> current_time('timestamp'),
));

//Check date validity
if( !is_float($data['date']) || $data['date'] <= 0 )
    return 0;

//Convert activity date from local timestamp to GMT mysql format
$data['activity_date'] = date_i18n( 'Y-m-d H:i:s', $data['date'], true );

//Initialise column format array
$column_formats = test_get_log_table_columns();
//Force fields to lower case
$data = array_change_key_case ( $data );

//White list columns
$data = array_intersect_key($data, $column_formats);

//Reorder $column_formats to match the order of columns given in $data
$data_keys = array_keys($data);
$column_formats = array_merge(array_flip($data_keys), $column_formats);
 $wpdb->insert($wpdb->wptuts_activity_log, $data, $column_formats);
return $wpdb->insert_id;

}
 function retrieve_val_data() {
global $wpdb;

 }

And Is it possible to add, delete, and update datas in tables via the front end? I mean adding, editing, deleting data values after activating the plugin and from a seperate menu page.

Related posts

1 comment

  1. Yes, it is possible to add, update and delete data in custom tables with a plugin.

    When you run your insert_data_into_table() function, what errors do you get?

Comments are closed.