Title: Adding data to table (database)
Last modified: August 19, 2016

---

# Adding data to table (database)

 *  Resolved [nemci7v](https://wordpress.org/support/users/nemci7v/)
 * (@nemci7v)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/adding-data-to-table-database/)
 * I have a custom table in my WordPress database named “post_votes” used for voting
   posts.
 * Whenever I publish a new post can I simultaneously add some data to that table
   as well?
 * I’m trying to achieve this without editing any wp core files. Perhaps a function
   if possible.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Anonymous User
 * (@anonymized-3085)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/adding-data-to-table-database/#post-1774631)
 * You’d need to hook into save_post with an `add_action('save_post','yourfunction');`
 *  Thread Starter [nemci7v](https://wordpress.org/support/users/nemci7v/)
 * (@nemci7v)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/adding-data-to-table-database/#post-1774686)
 * Thank you for the reply!!! I’m finally on the right track 🙂
 * I tried this in my plugin file
 *     ```
       function test_it($post_ID)  {
       	$wpdb->insert( 'post_votes', array( 'post_id' => $post_ID, 'up' => 0, 'down' => 0 ), array( '%s', '%d' ) )
           return $post_ID;
       }
   
       add_action ( 'publish_post', 'test_it' );
       ```
   
 * and when I activate teh plugin I get
 * > Parse error: syntax error, unexpected T_RETURN
 * on the function.. not sure if I’m doing this right
 *  Thread Starter [nemci7v](https://wordpress.org/support/users/nemci7v/)
 * (@nemci7v)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/adding-data-to-table-database/#post-1774688)
 * nvm I forgot to put a semicolon.. but now when I try and publish a new post I
   get
 * > Fatal error: Call to a member function insert() on a non-object
 * for
 * `$wpdb->insert( 'post_votes', array( 'post_id' => $post_ID, 'up' => 0, 'down'
   => 0 ), array( '%s', '%d' ) )`
 *  Anonymous User
 * (@anonymized-3085)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/adding-data-to-table-database/#post-1774722)
 * add global $wpdb; to your function.
 *  Thread Starter [nemci7v](https://wordpress.org/support/users/nemci7v/)
 * (@nemci7v)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/adding-data-to-table-database/#post-1774774)
 * yes that did it! thanks again!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Adding data to table (database)’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [tables](https://wordpress.org/support/topic-tag/tables/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [nemci7v](https://wordpress.org/support/users/nemci7v/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/adding-data-to-table-database/#post-1774774)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
