"Gilles Ganault" <no****@nospam. comwrote in message
news:q5******** *************** *********@4ax.c om...
Hello
Out of curiosity, is there a smarter, easier way to read data sent by
a form, and save them into a database? I have about 20 fields, and
it'd be easier if I could just use a loop to go through an array and
generate the SQL query in a couple of lines:
here's one way...certainly not the only way. however, this demonstrates what
you ask...sorry for the text-wrapping:
<?
$date = strtotime('now' );
$date = date('m', $date) . '/01/' . date('Y', $date);
$date = $_REQUEST['date'] ? $_REQUEST['date'] : $date;
$date = strtotime($date );
$dealer = 'someFacility';
// define updateable fields and default their values
$financials = array(
'Center' =$dealer
,
'Name' =$dealerName
,
'Month' =date('m', $date)
,
'Year' =date('Y', $date)
,
'pbsMaterialInv entory' =0 ,
'bcsAdvertising ' =0
);
// used to create dynamic db update statements
function createUpdateSta tement(&$value, $key)
{
$value = $key . " = '" . $value . "'";
return $value;
}
// used to format field values
function formatValue(&$v alue, $key = '', $request = array())
{
if (!is_array($req uest)){ $request = array($request) ; }
if (in_array($key, array('Center', 'Name', 'Month', 'Year'))){ return; }
if ($request){ $value = $request[$key]; }
$value = number_format(g etInteger($valu e));
return $value;
}
// whatever $value is, format it as an integer regarless of locale...
// inval may truncate commas...we're just forcing correct truncation
function getInteger(&$va lue, $key = '', $request = array())
{
if (!is_array($req uest)){ $request = array($request) ; }
if (in_array($key, array('Center', 'Name', 'Month', 'Year'))){ return; }
if ($request){ $value = $request[$key]; }
$locale = localeconv();
$decimal = $locale['decimal_point'] . $locale['mon_decimal_po int'];
$negative = $locale['negative_sign'];
$value = intval(preg_rep lace('/[^0-9' . $decimal . $negative . ']/',
'', $value));
return $value;
}
// update $financials with values provided by user submit
array_walk($fin ancials, 'getInteger', $_REQUEST);
// sledge-hammer db update
$sql = "
DELETE
FROM financials
WHERE Center = '" . $dealer . "'
AND STR_TO_DATE(CON CAT(Year, '-', Month, '-01'), '%Y-%m-%d')
=
STR_TO_DATE('" . date('Y-m-d', $date) . "', '%Y-%m-%d')
";
db::execute($sq l);
$sql = "
INSERT INTO
financials
(
" . implode(",\r\n ", array_keys($fin ancials)) . "
)
VALUES
(
'" . implode("',\r\n '", $financials) . "'
)
";
db::execute($sq l);
// more gentle update where we have determined a record exists alread
array_walk($fin ancials, 'createUpdateSt atement');
$sql = "
UPDATE financials
SET
" . implode(",\r\n ", $supplement) . "
WHERE Center = '" . $dealer . "'
";
db::execute($sq l);
// now, verify in debug that data made it into the db
// having used either sledge-hammer or gentle upate
$sql = "
SELECT " . implode(",\r\n ",
array_keys($fin ancials)) . "
FROM financials
WHERE Center = '" . $dealer .
"'
AND STR_TO_DATE(CON CAT(Year, '-', Month, '-01'),
'%Y-%m-%d') =
STR_TO_DATE('" . date('Y-m-d', $date) . "',
'%Y-%m-%d')
";
$records = db::execute($sq l);
$financials = $records[0];
array_walk($fin ancials, 'formatValue');
echo '<pre style="font:10p x;">' . print_r($financ ials, true) . '</pre>';
?>