Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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?

share|improve this answer
    
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. –  vickythegme Aug 29 '13 at 7:03
    
Have you read this: Creating Tables with Plugins? What problems are you having, specifically? –  J.D. Aug 29 '13 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? –  vickythegme Aug 30 '13 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 '13 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? –  vickythegme Aug 31 '13 at 2:03
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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