Quote from: pixelchutes at Oct 20, 2006, 12:21 AM
Query a table’s MetaData using $modx->db->getTableMetaData, followed by an update query on only the form fields that are named after table columns!
In this quick example, I grab the columns from our
`leads` table, and for any form field submitted that matches a column name in the table, a definition is defined for that column, using the validated, submitted form value.
(NOTE: I am not passing {PREFIX} because I don’t use one here)
// Grab our table's columns
$tblColumns = array_keys( $modx->db->getTableMetaData( 'leads' ) );
// Loop through each column, and insert it if present in the form
foreach( $tblColumns as $tblCol )
if( isset( $fields[ $tblCol ] ) ) $dbTable[ $tblCol ] = $fields[ $tblCol ]
However, my contact form asks for both
Best Time to Call and whether in
AM or
PM. I also need to convert the time they submitted the form to be MySQL-insert-friendly.
So, I handle any custom processing immediately after:
// Custom processing (e.g. date values, field joining, PHP logic, etc)
// Insert field/value pairs to insert/update in our table
$dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) );
$dbTable[call_time] = $fields[time] . ' ' . $fields[amorpm];
And then, simply invoke the insert SQL (as in my original example):
$dbQuery = $modx->db->insert( $dbTable, 'leads' );
And here is the result...eForm records right in the db!