Formidable's knowledge base is a valuable resource with a lot of very good coding examples. One in a while though, you run into a code example that doesn't quite work as intended. Such is the case with the example for updating a field value with PHP.
The problem with the knowledge base code example has to do with the FrmEntryMeta::add_entry_meta() function. This function does not check if an entry meta has been added or not. So if you use the function as shown in the knowledge base example, it will add the same values over and over again every time you test your code.
The knowledge base example for FrmEntryMeta::update_entry_meta() works as shown. The one caveat with this function, is that if the field is not already added to the frm_item_metas table, it will not generate an error.
The following code example is used to add a user id to a form entry when using the Formidable User Registration add-on. The function runs in the frmreg_after_create_user hook.
This code first checks if the field exists in the frm_item_metas table for a particular entry. A field will not exist in this table if it was never given a value when the entry was created. Providing a default value for the field will ensure that it is written to the table.
If the field doesn't exist, this code adds it. If it does exist, this code updates it.
Leave a Reply