ChronoForms Saving To A Database Table
ChronoForms Saving To A Database Table
net
ChronoForms How-to doc Saving to a database table using bind, store and save
ChronoForms DB Connection feature provides and easy way of saving form data to a database table. However, sometimes you need to create custom code to save or update a record. Level: advanced, requires some PHP, MySQL and Joomla! knowledge. This How-to document links to the recipe Creating a table to save your results and linking your form to it in Chapter 4 Saving Form Data in the Database of the ChronoForms 1.3 for Joomla! sites cookbook.
CHRONO
Forms
Greyhead.net
Greyhead.net
forms_newsletter_signup table. Save the form without making any other changes. Go to your MySQL Administrator (usually PHPMyAdmin) and browse in the jos_chronoforms table, go to the last record, and find the dbclasses column. Or, if you prefer, use this MySQL query (changed for your database and table):
SELECT `dbclasses` FROM `jos_chrono_contact` WHERE `name` = 'temp_form'
} ?>
Greyhead.net
You can see that inside the if clause there is a Class created with one variable defined for each column in the table and a function to call the Joomla! JTable Class to create a Table Object. X The Object has the variables defined here and some standard methods. There are twenty or so methods but only a few concern us here: bind and store, and later save, being the main ones. Once we have this code safely copied and pasted, the temp_form can be deleted, its work is done.
Greyhead.net
Bind the two together so that the data is linked into the Object Store the Object which will save the data into the database record. If the value of the primary key column exists in the database then the existing record will be updated; otherwise a new record will be created. X Note: For this to work one of the database columns must be defined as the primary key. If ChronoForms created the table it will be the cf_id column as in the example above. The primary key should also be an integer some of the Joomla! code fails if it is a string. Lets try an example: well update the name of a newsletter subscriber assuming that we already know the cf_id and the new name:
<?php $data = array(); $data['cf_id'] = 99; $data['name'] = 'John Smith'; $record =& JTable::getInstance("chronoforms_newsletter_signup", "Table"); $record->bind($data); $record->store(); ?>
Thats all that is needed to update the table assuming of course that all goes well with the store. We can add some Error reporting to this to capture anything that goes amiss.
if ( !$record->bind($data) ) { JError::raiseWarning(100, $record->getError());
Greyhead.net
} if ( !$record->store() ) { JError::raiseWarning(100, $record->getError()); }
This uses the Joomla! Error logging system but you could also output message to the user if that would be useful.
A simpler way . . .
The code so far is pretty much what you will find in the ChronoForms Autogenerated code box. But as I looked a little deeper to write this I found a simpler way. As with many simpler things it hides some of the underlying process so I've left it to this point to use it. The Joomla! Table Object has a save method that combines both bind and store. X It combines some other methods too: check, checkin and reorder. We won't spend any time on them here except to note what they do: check allows some predefined validation or processing of the results before they are saved (very useful for date handling for example); checkin is used with checkout to lock rows for processing if there is a possibility of two users trying to update the same record at the same time (this requires checked_out and checked_out_time columns in the table; and
Greyhead.net
reorder which is used to clean up the ordering sequence of the records (only useful if your table has an ordering column). Going back to our code example, we can now use
. . . $record =& JTable::getInstance("chronoforms_newsletter_signup", "Table"); $record->save($data); ?>
Greyhead.net
var var var var $ipaddress = null; $cf_user_id = null; $name = null; $email = null;
} ?>
X If you are not working with a table created by ChronoForms then some further changes may well be needed to match the table structure.
Greyhead.net
Bob Janes is the author of the ChronoForms 1.3 for Joomla! site Cookbook published by Packt Publishing. He provides support on the ChronoEngine forums and works as a developer of custom applications (often using ChronoForms and ChronoConnectivity) for Joomla! and WordPress. Bob can be contacted at [email protected] ChronoConnectivity is a Joomla! Component that works with ChronoForms to allow the easy display of lists of records from a database table. ChronoConnectivity and ChronoForms are published by ChronoEngine.com. This help-sheet was written for ChronoConnectivity 2.0 RC3 ChronoEngine Tutorials by Bob Janes are licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License Disclaimer To the best of our knowledge and belief the information in this tutorial was substantially correct at the time of writing. Neither the author, nor ChronoEngine.com shall have neither liability nor responsibility in respect of any loss or damage caused, or alleged to have been caused, directly or indirectly, by the information contained in this document.
Greyhead.net
Greyhead.net
10
Greyhead.net
11
Greyhead.net
12
Greyhead.net
13
Greyhead.net
14
Greyhead.net
15