You are on page 1of 16

Greyhead.

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.

How-to docs and tutorials

CHRONO
Forms

Greyhead.net

Saving to a database table using bind, store and save


The usual way of saving data to a MySQL database table is to build a query using INSERT (for a new record); UPDATE (to modify a record); or REPLACE (which merges the two). The queries typically require you to set out a long list of column names and values to be saved and are quite laborious to create accurately and reliably. Joomla! has some methods that can help short-cut writing these queries and give us greater flexibility in saving data. These methods are used by ChronoForms and you can see parts of the code in the Autogenerated Code tab of a form with a DB Connection. Here well build up the code we need bit by bit.

Getting the table structure


We need to know the structure of the database table so that its clear which bits of data belong in which column. The information that we need is not very complicated but it does need to be correct. Fortunately we can persuade ChronoForms to create it for us. For this example Ill use a newsletter sign-up table; exactly the same process can be used with any other Joomla! table (though, as always, take great care before working with any of the core Joomla! tables). In the ChronoForms Forms Manager click the New icon, give the form a name e.g. temp_form, open the Form in the Form Editor, click the DB Connection tab and create a Connection to the jos_chrono-

Saving to a database table using bind, store and save

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'

The code that I find is this (with line-breaks added):


<?php if (!class_exists('Tablechronoforms_newsletter_signup')) { class Tablechronoforms_newsletter_signup extends JTable { var $cf_id = null; var $uid = null; var $recordtime = null; var $ipaddress = null; var $cf_user_id = null; var $name = null; var $email = null; function __construct( &$database ) { parent::__construct( 'jos_chronoforms_newsletter_signup', 'cf_id', $database ); }

} ?>

Saving to a database table using bind, store and save

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.

Using the code


This code can be used in any of the ChronoForms boxes that allow PHP: thats pretty much all of them except the JavaScript and CSS boxes. Well assume that we are working in the OnSubmit After Email box here. We need the code above, and that can conveniently be tucked away at the end of the box with our working code before it. The structure of the working code is like this: Gather the data we want to save into an array. This data can be from form results, from other database queries, or from calculations. The only requirement is that the array keys should match the column names. You dont have to include every column existing data will be kept if the record is updated; and you can have array keys that arent included in the Object they will just be ignored. Get an instance of the Table Object

Saving to a database table using bind, store and save

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());

Saving to a database table using bind, store and save

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

Saving to a database table using bind, store and save

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); ?>

The full example


Putting it all together our code now looks like this. The highlighted text will need to be changed to match the table and column names you are using:
<?php $data = array(); $data['cf_id'] = 99; $data['name'] = 'John Smith'; $record =& JTable::getInstance("chronoforms_newsletter_signup", "Table"); if ( !$record->save($data) ) { JError::raiseWarning(100, $record->getError()); } if (!class_exists('Tablechronoforms_newsletter_signup')) { class Tablechronoforms_newsletter_signup extends JTable { var $cf_id = null; var $uid = null; var $recordtime = null;

Saving to a database table using bind, store and save

Greyhead.net
var var var var $ipaddress = null; $cf_user_id = null; $name = null; $email = null;

} ?>

function __construct( &$database ) { parent::__construct( '#__chronoforms_newsletter_signup', 'cf_id', $database ); }

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.

Bob Janes 27 September 2010

Saving to a database table using bind, store and save

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 info@greyhead.net 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.

Saving to a database table using bind, store and save

Greyhead.net

Saving to a database table using bind, store and save

Greyhead.net

10

Saving to a database table using bind, store and save

Greyhead.net

11

Saving to a database table using bind, store and save

Greyhead.net

12

Saving to a database table using bind, store and save

Greyhead.net

13

Saving to a database table using bind, store and save

Greyhead.net

14

Saving to a database table using bind, store and save

Greyhead.net

15

Saving to a database table using bind, store and save

You might also like