Title: Dynamically insert data with php code
Last modified: August 18, 2023

---

# Dynamically insert data with php code

 *  Resolved [alexshum939](https://wordpress.org/support/users/alexshum939/)
 * (@alexshum939)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/)
 * Hello,
 * First of all, this is a great plugin, thanks for making it and for providing 
   tech support here. But anyway is a way to populate the table with php code?
 * In my scenario, I have a columns that requires human input, and 2 columns that
   are left empty and requires user interaction to populate them then save the user
   generated data to Tablepress’s db in the backend. Is that possible?
 * Thanks!

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

 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-16984028)
 * Hi,
 * thanks for your post, and sorry for the trouble.
 * I see what you mean, but this is not that trivial, I’m afraid. Essentially, you
   will need your own form handling, but could then maybe use the same code structure
   that TablePress uses when saving the table, e.g. by inspecting the controller-
   admin_ajax.php function.
 * Regards,
   Tobias
 *  Thread Starter [alexshum939](https://wordpress.org/support/users/alexshum939/)
 * (@alexshum939)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-16984949)
 * Oof thats a bummer, how about just directly insert the user generated data into
   the db using WP’s sql functionalities? I just have to hunt down the db tables
   that Tablepress uses to store my tables? Will that be easier?
 * Thanks for the respond by the way!
    -  This reply was modified 2 years, 9 months ago by [alexshum939](https://wordpress.org/support/users/alexshum939/).
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-16985268)
 * Hi,
 * well, TablePress does not use SQL tables directly (but it uses a WordPress “Custom
   Post Type”), so that using direct SQL queries will not work.
   The PHP functions
   are not that complex though.
 * Now, of course, depending on how many such entries you’ll need using your own
   SQL table might be worth a shot.
 * Regards,
   Tobias
 *  Thread Starter [alexshum939](https://wordpress.org/support/users/alexshum939/)
 * (@alexshum939)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-16985334)
 * Thanks for the reply again and sorry for the confusion,
 * I figured out that the save method in TablePress::$model_table->save is probably
   what I want, but some reason it keeps on giving me error, I tried both passing
   the save method with the table array that I obtained from TablePress::$model_table-
   >load except I replaced the ‘data’ attribute with the new table data array, and
   also tried constructing a new table array with the same ‘id’ attribute as before
   and ‘data’ using the new table array. I am still new to wordpress, so IDK how
   to debug properly and i am calling the save function in the backend from the 
   frontend on the client side through a rest api I defined, so the only error message
   I got is something along the line of “something in the site went wrong, error
   code: 500”. I had the ‘options’ and ‘visibility’ subarray to be empty, but I 
   dont think they are a factor?
 * In addition, once I am able to successfully save the data is there a way for 
   me to re-fetch or re-render the table, so the new saved data is able to be display
   on my webpage?
    -  This reply was modified 2 years, 9 months ago by [alexshum939](https://wordpress.org/support/users/alexshum939/).
    -  This reply was modified 2 years, 9 months ago by [alexshum939](https://wordpress.org/support/users/alexshum939/).
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-16985808)
 * Hi,
 * if you are getting error 500, you should be able to find out more in the server’s
   error log file, as those are server errors.
 * Now, between `->load(...)` and `->save(...)` you will need to use the `->prepare(...)`
   method as well, which will take care of certain validations and ensures that 
   all data is complete.
 * Also, always check the return values of these functions, as they return `WP_Error`
   objects in case something is wrong. (Using `var_dump()` should be fine for starters.)
 * As for re-rendering the table: If a table is updated via `->save()`, subsequent
   calls to the Shortcode (`[table id=123 /]`) (or the block editor block) will 
   render the latest version of the table.
 * Regards,
   Tobias
 *  Thread Starter [alexshum939](https://wordpress.org/support/users/alexshum939/)
 * (@alexshum939)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-16986300)
 * I am able to achieve everything I want for my client. Danke sehr!
 * Also a side note to everyone reading this, the method to prepare a table for 
   saving is “prepare_table” instead of “prepare”
    -  This reply was modified 2 years, 9 months ago by [alexshum939](https://wordpress.org/support/users/alexshum939/).
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-16986319)
 * Hi,
 * awesome! That’s really great to hear!
 * Ah, my bad, `->prepare_table()` of course!
 * Best wishes,
   Tobias
 * P.S.: In case you haven’t, please rate TablePress [here](https://wordpress.org/support/plugin/tablepress/reviews/#new-post)
   in the plugin directory. Thanks!
 *  [xerviami](https://wordpress.org/support/users/xerviami/)
 * (@xerviami)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-17226650)
 * Dear Tobias,
 * I am pretty interested in that approach.
 * Is there any documentation about that method ?
 * I well found the load/prepare_table/save in the model-table.php file, but it 
   is pretty tought.
 * Thank you !
 * Xerviami
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-17226939)
 * Hi Xerviami,
 * no, sorry, there’s no direct documentation on this at this time, only that code,
   with its inline docs.
 * Best wishes,
   Tobias
 *  [xerviami](https://wordpress.org/support/users/xerviami/)
 * (@xerviami)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-17227198)
 * OK no problem and thank you for this awesome plugin !
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-17228021)
 * Hi,
 * sure, no problem, you are very welcome 🙂
 * Best wishes,
   Tobias

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

The topic ‘Dynamically insert data with php code’ is closed to new replies.

 * ![](https://ps.w.org/tablepress/assets/icon.svg?rev=3192944)
 * [TablePress - Tables in WordPress made easy](https://wordpress.org/plugins/tablepress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/tablepress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/tablepress/)
 * [Active Topics](https://wordpress.org/support/plugin/tablepress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/tablepress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/tablepress/reviews/)

 * 11 replies
 * 3 participants
 * Last reply from: [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/dynamically-insert-data-with-php-code/#post-17228021)
 * Status: resolved