• Hi,
    I am new to WordPress, but fine with html, php, js etc. I have pages written in html / php that I now need to move onto ta a WordPress site – primarily data tables – users creating / deleting / updating from the front-end. after much frustration this seems difficult in WordPress(?)

    My initial problem is trying to get the user to add a record to a table, which in turn creates a new table (name based on some of the data entered + a random element);

    I have created a table that allows the user to add new records to the underlying WordPress database table (works fine – WpDataTables plugin). As the record is added I want to use data entered into one of the cells to create a NEW table in the database. Is this possible and if so any pointers to the process would be appreciated?

    Callbacks / actions and filters are phases I have come across which may help but cannot find any in depth examples for how to use them / where to store code and how to trigger them based on used action.

    Regards

    kjjf

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, you can create new tables. Use the global DB connection object $wpdb to execute the desired SQL query.

    Filters and actions are triggered by the code applying them. You generally don’t do actions or apply filters yourself, WP core code normally does that. What you do is add action or filter callback code to specific filter or action hooks that should execute when WP triggers that particular filter or action. More on filter and action hooks in the Plugin Handbook.

    Thread Starter kjjf

    (@kjjf)

    Hi bcworkz,

    thanks for taking the time to respond.

    Just to clarify, is it possible to ‘trigger’ some piece of code AFTER a record has been added to the database by the user. The aim of the code is to create a table based on data entered by the user – I cannot see how I can create the trigger.

    Thanks again

    KJJF

    Moderator bcworkz

    (@bcworkz)

    Yes, with a caveat. For example, the “save_post” action fires after the post update query has been sent. The appropriate hook depends on what code is used to add data. However, it’s impractical to query the database for added data immediately after adding. Writing data takes time. In the mean time PHP keeps executing, creating a race condition where the data might not yet be readable even though PHP has already sent the UPDATE or INSERT request.

    If your code is able to access the data PHP used to query the DB, such as from $_POST, reading from the DB is not needed and you can add a table or whatever. If the situation requires reading data that was just added, you could add a Cron job or the WP equivalent that does the additional work a short while later after the data has become readable.

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

The topic ‘Executing sql statements when adding new records to a table’ is closed to new replies.