Changed at 30 August 12 / 19:11
Hello, I made some modifications so that I could use with MSSQL 2000 database, I believe that these changes should have no consequences to others as MYSQL.
In class.dbFormHandler.php:
<?php
function _getQuery( $table, $data, $sqlFields, $edit, $keys = null )
{
// get the field names from the table
$fieldNames = $this->_db->getFieldNames( $this->_table );
// get the keys field names from the table
$keys = $this->_db->getPrKeys( $this->_table );
// check if we got the fieldnames
if( !$fieldNames )
{
trigger_error(
'Could not fetch the fieldnames of the table '. $this->_table,
E_USER_WARNING
);
return false;
}
// walk the data from the form
foreach( $data as $field => $value )
{
// does the field exists in the table?
if( !in_array($field, $fieldNames) )
{
// field does not exists, remove it from the data array (we dont need it )
unset( $data[$field] );
}
// does the PK field exists in the table?
elseif( in_array($field, $keys) )
{
// field exists, remove it from the data array (we dont need it )
unset( $data[$field] );
}
// the field exists in the table
else
{
// is the value an array? Implode it!
if( is_array($value) )
{
$value = implode(', ', $value);
}
// remove spaces etc
$value = trim( $value );
// if the value is not a SQL function...
if(!in_array($field, $sqlFields) )
{
// escape the value for saving it into the database
$value = $this->_db->escapeString( $value );
// // is the value a number or float?
// if( preg_match('/^-?\d*\.?\d+$/', $value))
// {
// // do we have to quote it ?
// if( $this->_db->quoteNumbers() )
// {
// $value = "'".$value."'";
// }
// }
// // the value is not a number.. just quote it
// else
// {
$value = "'".$value."'";
// }
// save the value. It's now db ready
$data[$field] = $value;
}
}
}
// check if there is still something left to save...
if( sizeof( $data ) == 0 )
{
return false;
}
// if it's an edit form
if($edit)
{
// generate the update query
$query = 'UPDATE '.$this->_db->quote( $this->_table )." SET \n";
// walk all fields and add them to the query
foreach($data as $field => $value)
{
$query .= ' '.$this->_db->quote( $field ).' = '.$value.", \n";
}
// add the where clause to the query
$query = substr($query, 0, -3) . $this->_getWhereClause();
}
// the form is an insert form..
else
{
// generate the insert query
$query = 'INSERT INTO '.$this->_db->quote( $this->_table )." (\n";
// add the field names to the query
foreach( array_keys($data) as $field )
{
$query .= ' '. $this->_db->quote( $field ) .", \n";
}
// add the values to the query
$query = substr($query, 0, -3) . ") VALUES (\n ";
$query .= implode(",\n ", $data)."\n);";
}
// return the query
return $query;
}
?>