1

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.

1 Answer 1

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?

6
  • I got the table installed and its datas too. I have tried inserting data from the admin panel. I haven't tried creating db tables from the admin panel via plugins as since I haven't seen any plugins with that functionality. And that is the reason I posted as a question here. Thanks for your effort though. Aug 29, 2013 at 7:03
  • Have you read this: Creating Tables with Plugins? What problems are you having, specifically?
    – J.D.
    Aug 29, 2013 at 12:29
  • I read them and I learned to create a custom table too. What I meant is can we create a table by giving the table field details through a seperate settings page which we create in the admin panel? Aug 30, 2013 at 8:29
  • Yes, that's certainly possible to do. You just need add your settings page a form, and then when it is submitted build the SQL CREATE TABLE query dynamically, and make sure that everything is properly validated and sanitized. If you need help doing that, then you need to just start doing it, and then ask specific questions here when you run into problems. We are here to answer questions, not write all of your code for you :-)
    – J.D.
    Aug 30, 2013 at 12:39
  • I don't need anyone to write my code if that's what you're talking about. I just wanted to know whether its possible or not since I have never seen any plugin similar to it. I have tried it before and now I ll try it even more. Can you link me to any similar plugin that creates tables via settings page? Aug 31, 2013 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.